5

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

Community
  • 1
  • 1
Alcott
  • 17,905
  • 32
  • 116
  • 173
  • 1
    And many, many others -- please search first. [how-does-pythons-super-do-the-right-thing](http://stackoverflow.com/questions/607186/how-does-pythons-super-do-the-right-thing) [how-does-pythons-super-work-with-multiple-inheritance](http://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance) [usage-of-python-3-super](http://stackoverflow.com/questions/2771904/usage-of-python-3-super) – agf Aug 21 '11 at 04:43

1 Answers1

5

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.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151