Class method differences in Python: bound, unbound and static

后端 未结 13 1290
日久生厌
日久生厌 2020-11-22 08:54

What is the difference between the following class methods?

Is it that one is static and the other is not?

class Test(object):
  def method_one(self)         


        
13条回答
  •  一向
    一向 (楼主)
    2020-11-22 09:37

    The call to method_two will throw an exception for not accepting the self parameter the Python runtime will automatically pass it.

    If you want to create a static method in a Python class, decorate it with the staticmethod decorator.

    Class Test(Object):
      @staticmethod
      def method_two():
        print "Called method_two"
    
    Test.method_two()
    

提交回复
热议问题