Static methods in Python?

后端 未结 10 761
悲&欢浪女
悲&欢浪女 2020-11-22 02:36

Is it possible to have static methods in Python which I could call without initializing a class, like:

ClassName.static_method()
10条回答
  •  遇见更好的自我
    2020-11-22 02:41

    Yes, check out the staticmethod decorator:

    >>> class C:
    ...     @staticmethod
    ...     def hello():
    ...             print "Hello World"
    ...
    >>> C.hello()
    Hello World
    

提交回复
热议问题