| title | Python staticmethod() built-in function - Python Cheatsheet |
|---|---|
| description | Transform a method into a static method. |
You can turn a class method into a static method by applying the @staticmethod decorator to a function in a class. For example:
>>> class C:
>>> @staticmethod
>>> def function(): ....Static methods can be called on the class itself like
>>> class Class:
>>> @staticmethod
>>> def function():
>>> print("X")
>>>
>>> Class.function()
>>> # XOr on an instance of the class like
>>> class Class:
>>> @staticmethod
>>> def function():
>>> print("X")
>>>
>>> new_class = Class()
>>> new_class.function()
>>> # XThe @staticmethod is in the form of a decorator the basics being it is a function that will return another function. This will be documented at a later time.