单个参数super() - 它的用途是什么?

时间:2012-07-02 09:22:29

标签: python superclass

  

可能重复:
  Usage of Python 3 super()

在Python 3.2的文档中,它说;

  

如果省略第二个参数,则返回的超级对象是未绑定的

根据我的理解,未绑定(如'unbound-to-instance')对象是从super(class,class)返回的对象。那么,超级(类)中的'未绑定'是什么意思?你怎么绑它?

class Base:
    def printme(self): print("I'm base")

class Derived(Base):
    def printme(self): print("I'm derived")

class Derived2(Derived):
    def printme(self):
        super().printme()
# next-in-mro bound-to-self method

        super(Derived, self).printme()
# beyond-Derived-in-mro bound-to-self method

        super(Derived, Derived2).printme(self)
# beyond-Derived-in-mro-of-Derived2 unbound method

        super(Derived2).printme
# WTF is this? There's not even a printme attribute in it

Derived2().printme()

0 个答案:

没有答案