Python - why can I call a class method with an instance?

让人想犯罪 __ 提交于 2020-05-08 06:53:07

问题


New to Python and having done some reading, I'm making some methods in my custom class class methods rather than instance methods.

So I tested my code but I hadn't changed some of the method calls to call the method in the class rather than the instance, but they still worked:

class myClass:
   @classmethod:
   def foo(cls):
      print 'Class method foo called with %s.'%(cls)

   def bar(self):
      print 'Instance method bar called with %s.'%(self)

myClass.foo()
thing = myClass()
thing.foo()
thing.bar()

This produces:

class method foo called with __main__.myClass.
class method foo called with __main__.myClass.
instance method bar called with <__main__.myClass instance at 0x389ba4>.

So what I'm wondering is why I can call a class method (foo) on an instance (thing.foo), (although it's the class that gets passed to the method)? It kind of makes sense, as 'thing' is a 'myClass', but I was expecting Python to give an error saying something along the lines of 'foo is a class method and can't be called on an instance'.

Is this just an expected consequence of inheritance with the 'thing' object inheriting the foo method from its superclass?

If I try to call the instance method via the class:

myClass.bar()

then I get:

TypeError: unbound method bar() must be called with myClass instance...

which makes perfect sense.


回答1:


You can call it on an instance because @classmethod is a decorator (it takes a function as an argument and returns a new function).

Here is some relavent information from the Python documentation

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

There's also quite a good SO discussion on @classmethod here.




回答2:


Let's start with a quick overview of the descriptor protocol. If a class defines a __get__ method, an instance of that class is a descriptor. Accessing a descriptor as the attribute of another object produces the return value of the __get__ method, not the descriptor itself.

A function is a descriptor; this is how instance methods are implemented. Given

class myClass:
    @classmethod:
    def foo(cls):
        print('Class method foo called with %s.' % (cls,))

    def bar(self):
        print 'Instance method bar called with %s.'%(self)

c = myClass()

the expression c.bar is equivalent to

myClass.__dict__['bar'].__get__(c, myClass)

while the expression myClass.bar is equivalent to the expression

myClass.__dict__['bar'].__get__(None, myClass)

Note the only difference is in the object passed as the first argument to __get__. The former returns a new method object, which when called passes c and its own arguments on to the function bar. This is why c.bar() and C.bar(c) are equivalent. The latter simply returns the function bar itself.

classmethod is a type that provides a different implementation of __get__. This means that c.foo() and myClass.foo() call __get__ as before:

# c.foo
myClass.__dict__['foo'].__get__(c, myClass)

# myClass.foo
myClass.__dict__['foo'].__get__(None, myClass)

Now, however, both calls return the same method object, and this method object, when called, passes myClass as the first argument to the original function object. That is, c.foo() is equivalent to myClass.foo(), which is equivalent to x(myClass) (where x is the original function defined before the decoration bound the name foo to an instance of classmethod).



来源:https://stackoverflow.com/questions/30190061/python-why-can-i-call-a-class-method-with-an-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!