Static methods in Python?
问题 Is it possible to have static methods in Python which I could call without initializing a class, like: ClassName.static_method() 回答1: Yep, using the staticmethod decorator class MyClass(object): @staticmethod def the_static_method(x): print(x) MyClass.the_static_method(2) # outputs 2 Note that some code might use the old method of defining a static method, using staticmethod as a function rather than a decorator. This should only be used if you have to support ancient versions of Python (2.2