To add a static method to a class in Python, you can use the staticmethod built-in function. A static method belongs to a class rather than an instance of the class, and can be called without creating an object of the class.
Here's an example:
main.py116 chars6 linesIn this example, my_static_method() is defined as a static method of the MyClass class using the @staticmethod decorator. Any arguments that are passed to the method are passed explicitly, without the self argument that is passed to instance methods.
To call a static method, you can use the class name followed by the method name:
main.py46 chars2 lines
Note that a static method does not have access to the instance or class state, and cannot modify any attributes or call any other methods that are not also static.
gistlibby LogSnag