开发者

[python]: confused by super() [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Understanding Python super()

Class B subclasses class A, so in B's __init__ we should call A's __init__ like this:

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

But with super(), I saw something like this:

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

My questions are:

    开发者_开发问答
  1. Why not super(B, self).__init__(self)? Just because the return proxy object is a bound one?

  2. If I omit the second argument in super and the return proxy object is an unbound one, then should I write super(B).__init__(self)?


super() returns an instance of the base class, so self gets implicitly passed to __init__() like in any other method call.

With regards to your second question, that's correct. Calling super() without an instance as the second argument returns a reference to the class itself, not an instance constructed from your subclass instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜