ScanSkill

staticmethod

This staticmethod() function returns a static method for function.

Syntax

staticmethod(function)

Here,

  • function: Required. Function to be decorated.

Note: A static method does not receive an implicit first argument. When a function decorated with @staticmethod is called, we don’t pass an instance of the class to it (as we normally do with methods). This means we can put a function inside a class but we can’t access the instance of that class (this is useful when your method does not use the instance).

Examples

>>> class Func1:
...     @staticmethod
...     def subfunc():
...         print 'hello'
...
>>> Func1.subfunc
<function subfunc at 0x00DBC1B0>
>>> Func1().subfunc
<function subfunc at 0x00DBC1B0>
>>> Func1.subfunc()
hello
>>> Func1().subfunc()
Hello
  • Without decorator
>>> # this example uses obsolete no-decorator syntax
>>> class Func1:
...     def subfunc():
...         print 'hello'
...     subfunc = staticmethod(subfunc)
...
>>> Func1.subfunc
<function subfunc at 0x00DBC270>
>>> Func1().subfunc
<function subfunc at 0x00DBC270>
>>> Func1().subfunc()
hello
>>> Func1.subfunc()
hello*