Python Static method
Static Method:
The static method in python class is achieved by using built-in decorator called @staticmethod or staticmethod()
By using static method we can call the method in class with or without creating an object. Which means that static method is bound to a class rather than the objects of that class.
Since creating object is not necessary in static method we no need to include self as parameter in methods.
Note: As self is the reference of object
Using @staticmethod in class:
class Lion:
@staticmethod # create static method for portray using decorator
def portray():
print('Lion is king of the jungle')
Lion.portray() # with out creating object calling the method
obj = Lion() # creating object
obj.portray() # calling the method using objectOutput:
class Lion:
def portray():
print('Lion is king of the jungle')
Lion.portray =staticmethod(Lion.portray) # create static method for portray
Lion.portray() # with out creating object calling the method
obj = Lion() # creating object
obj.portray() # calling the method using objectOutput:


Comments
Post a Comment