[python]:被super()困惑

可能重复:
了解Python超级()

B类的子类A ,所以在B的__init__我们应该像这样调用A的__init__

class B(A):
    def __init__(self):
        A.__init__(self)  

但是用super() ,我看到了这样的东西:

class B(A):
    def __init__(self):
        super(B, self).__init__()  #or super().__init__()

我的问题是:

  • 为什么不是super(B, self).__init__(self) ? 仅仅因为返回代理对象是绑定的对象?

  • 如果我在super中省略第二个参数,并且返回代理对象是非绑定的,那么我应该写super(B).__init__(self)


  • super()返回基类的一个实例,所以self就像其他方法调用一样被隐式地传递给__init__()

    关于你的第二个问题,这是正确的。 在没有实例的情况下调用super()作为第二个参数将返回对类本身的引用,而不是从您的子类实例构造的实例。

    链接地址: http://www.djcxy.com/p/30033.html

    上一篇: [python]: confused by super()

    下一篇: Git is not ignoring file mode changes (chmod), why?