-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic65.py
More file actions
33 lines (31 loc) · 1.29 KB
/
static65.py
File metadata and controls
33 lines (31 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#Static methods in Python are methods that belong to a class rather than an instance of the class.
#defined using the @staticmethod decorator and do not have access to the instance of the class (i.e. self)
class Car:
@staticmethod
def start_engine():
print("Engine started")
Car.start_engine() # Output: Engine started
#Static methods can be called on the class itself, without creating an instance of the class.
#This is useful for utility functions that are related to the class but do not require access to instance-specific data.
#Static methods can also be used to create factory methods that return instances of the class.
class Car:
@staticmethod
def create_car(make, model):
return Car(make, model)
def __init__(self, make, model):
self.make = make
self.model = model
def info(self):
return f"Car make: {self.make}, model: {self.model}"
object1 = Car.create_car("Toyota", "Camry")
print(object1.info())
#Static methods can also be used to create utility functions that are related to the class but do not require access to instance-specific data.
class MathUtils:
@staticmethod
def add(a, b):
return a + b
@staticmethod
def subtract(a, b):
return a - b
print(MathUtils.add(5, 3)) # Output: 8
print(MathUtils.subtract(5, 3)) # Output: 2