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

a 夏天 提交于 2020-01-13 11:29:07

问题


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)?


回答1:


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.



来源:https://stackoverflow.com/questions/7136406/python-confused-by-super

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