Python uses staticmethod Define a static method
In Python,staticmethod is a decorator used to define a static method. Static methods do not depend on an instance of the class, nor on the class itself. They are usually used to perform operations related to the class but independent of class or instance state.
Below is an example using staticmethod Example of defining a static method:
Example
class
MyClass:
@
staticmethod
def
my_static_method
(
)
:
return
"This is a static method."
Call the static method
result
=
MyClass.
my_static_method
(
)
(
result
)
Code analysis:
@staticmethodis a decorator used to mark themy_static_methodmethod as a static method.my_static_methodThe method does not receive aselforclsparameter because it does not depend on an instance of the class or on the class itself.- A static method can be called directly through the class name without creating an instance of the class.
Output result:
This is a static method.
Python3 Examples