Static methods in Python?

后端 未结 10 760
悲&欢浪女
悲&欢浪女 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:46

    So, static methods are the methods which can be called without creating the object of a class. For Example :-

        @staticmethod
        def add(a, b):
            return a + b
    
    b = A.add(12,12)
    print b
    

    In the above example method add is called by the class name A not the object name.

提交回复
热议问题