Class method differences in Python: bound, unbound and static

后端 未结 13 1294
日久生厌
日久生厌 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:30

    In Python, there is a distinction between bound and unbound methods.

    Basically, a call to a member function (like method_one), a bound function

    a_test.method_one()
    

    is translated to

    Test.method_one(a_test)
    

    i.e. a call to an unbound method. Because of that, a call to your version of method_two will fail with a TypeError

    >>> a_test = Test() 
    >>> a_test.method_two()
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: method_two() takes no arguments (1 given) 
    

    You can change the behavior of a method using a decorator

    class Test(object):
        def method_one(self):
            print "Called method_one"
    
        @staticmethod
        def method_two():
            print "Called method two"
    

    The decorator tells the built-in default metaclass type (the class of a class, cf. this question) to not create bound methods for method_two.

    Now, you can invoke static method both on an instance or on the class directly:

    >>> a_test = Test()
    >>> a_test.method_one()
    Called method_one
    >>> a_test.method_two()
    Called method_two
    >>> Test.method_two()
    Called method_two
    

提交回复
热议问题