0
Please explain to me what static methods are?
I couldn't understand what static methods are. Can someone please give an explanation with an example
2 Answers
+ 1
the static method is similar to a class & can be passed with arguments but only can be invoked with argument that is already declared no new arguments at runtime are accepted.
0
If you applyĀ staticĀ keyword with anyĀ method, it is known asĀ static method. AĀ static methodĀ belongs to the class rather than object of a class. AĀ static methodĀ invoked without the need for creating an instance of a class.Ā static methodĀ can accessĀ staticĀ data member and can change the value of it.
Yep, using theĀ staticmethodĀ decorator
class MyClass(object):
@staticmethod
def the_static_method(x):
print x
MyClass.the_static_method(2)
# outputs 2
Note that some code might use the old method of defining a static method, usingĀ staticmethodĀ as a function rather than a decorator. This should only be used if you have to support ancient versions of Python (2.2 and 2.3)