1#To create a static method, just add "@staticmethod" before defining it.
2
3>>>class Calculator:
4 # create static method
5 @staticmethod
6 def multiplyNums(x, y):
7 return x * y
8
9>>>print('Product:', Calculator.multiplyNums(15, 110))
10Product:1650
1class Person:
2 def __init__(self, _name, _age):
3 self.name = _name
4 self.age = _age
5
6 def sayHi(self):
7 print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
8
9p1 = Person('Bob', 25)
10p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
1class uneclasse():
2 def __init__(self):
3 pass
4 def something(self):
5 pass
6xx = uneclasse()
7xx.something()
1class Fan:
2
3 def __init__(self, company, color, number_of_wings):
4 self.company = company
5 self.color = color
6 self.number_of_wings = number_of_wings
7
8 def PrintDetails(self):
9 print('This is the brand of',self.company,'its color is', self.color,' and it has',self.number_of_wings,'petals')
10
11
12 def switch_on(self):
13 print("fan started")
14
15 def switch_off(self):
16 print("fan stopped")
17
18 def speed_up(self):
19 print("speed increased by 1 unit")
20
21 def speed_down(self):
22 print("speed decreased by 1 unit")
23
24usha_fan = Fan('usha','skin',5)
25fan = Fan('bajaj','wite', 4)
26
27
28print('these are the details of this fan')
29usha_fan.PrintDetails()
30print()
31
32usha_fan.switch_on()
1from datetime import date
2
3# random Person
4class Person:
5 def __init__(self, name, age):
6 self.name = name
7 self.age = age
8
9 @classmethod
10 def fromBirthYear(cls, name, birthYear):
11 return cls(name, date.today().year - birthYear)
12
13 def display(self):
14 print(self.name + "'s age is: " + str(self.age))
15
16person = Person('Adam', 19)
17person.display()
18
19person1 = Person.fromBirthYear('John', 1985)
20person1.display()