Python static Method
python static method: a static method used to return a function that does not force the passing of arguments, declaring a static method as follows.
class C(object):
@staticmethod
def f(arg1, arg2, ...):
...
The above example declares the staticmethod f so that it can be instantiated using C().f()
, or of course it can be called without instantiating the method C.f()
Related course: Complete Python Programming Course & Exercises
Grammar
The grammar is
@staticmethod
def f(a,b,c):
...
Return value:
**None**
The following example shows how to use the staticmethod() function
class B(object):
@staticmethod
def f():
print('Hello World!');
B.f();
# staticmethod without instantiating
cobj = B()
cobj.f() # can also be called after instantiation
output
Hello World!
This is where the staticmethod() function was learned.