14

A colleague of mine wrote code analogous to the following today, asked me to have a look, and it took me a while to spot the mistake:

class A():                                                                                         
    def __init__(self):                                                         
        print('A')                                                              

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

b = B()

The problem here is that there's no self parameter to super() in B's constructor. What surprised me is that absolutely nothing happens in this case, i.e. no error, nothing. What does the super object created by super(B) contain? As an object, it clearly has a constructor, so that's what gets called, but how is that object related to B? In particular, why is this valid code and doesn't throw an exception somewhere? Is super(B) an object with some actual use and what would that be?

4
  • 1
    Are you in Python 2 or 3? Commented Jan 17, 2018 at 13:31
  • The documentation on super states that omitting the second argument causes it to create an unbound object Commented Jan 17, 2018 at 13:39
  • What's even more odd is that calling super(B).__init__() only works inside of a method of B (or a subclass of B). Calling super(B).__init__() outside of a class throws RuntimeError: super(): no arguments Commented Jan 17, 2018 at 13:59
  • Here's a very nice article about unbound super objects. Commented Jan 17, 2018 at 14:02

2 Answers 2

6

The only thing that causes all these ambiguities is that "why obj = super(B).__init__() works?". That's because super(B).__self_class__ returns None and in that case you're calling the None objects' __init__ like following which returns None:

In [40]: None.__init__()

Regarding the rest of the cases, you can simply check the difference by calling the super's essential attributes in both cases:

In [36]: class B(A):                                                                     
        def __init__(self):                                                         
                obj = super(B, self)
                print(obj.__class__)
                print(obj.__thisclass__)
                print(obj.__self_class__)
                print(obj.__self__)
   ....:         

In [37]: b = B()
<class 'super'>
<class '__main__.B'>
<class '__main__.B'>
<__main__.B object at 0x7f15a813f940>

In [38]: 

In [38]: class B(A):                                                                     
        def __init__(self):                                                         
                obj = super(B)
                print(obj.__class__)
                print(obj.__thisclass__)
                print(obj.__self_class__)
                print(obj.__self__)
   ....:         

In [39]: b = B()
<class 'super'>
<class '__main__.B'>
None
None

For the rest of the things I recommend you to read the documentation thoroughly. https://docs.python.org/3/library/functions.html#super and this article by Raymond Hettinger https://rhettinger.wordpress.com/2011/05/26/super-considered-super/.

Moreover, If you want to know why super(B) doesn't work outside of the class and generally why calling the super() without any argument works inside a class you can read This comprehensive answer by Martijn https://stackoverflow.com/a/19609168/2867928.

A short description of the solution:

As mentioned in the comments by @Nathan Vērzemnieks you need to call the initializer once to get the super() object work. The reason is laid behind the magic of new super object that is explained in aforementioned links.

In [1]: class A:
   ...:     def __init__(self):
   ...:         print("finally!")
   ...:

In [2]: class B(A):
   ...:     def __init__(self):
   ...:         sup = super(B)
   ...:         print("Before: {}".format(sup))
   ...:         sup.__init__()
   ...:         print("After: {}".format(sup))
   ...:         sup.__init__()
   ...:

In [3]: B()
Before: <super: <class 'B'>, NULL>
After: <super: <class 'B'>, <B object>>
finally!
Sign up to request clarification or add additional context in comments.

9 Comments

@Rawing That's strongly related to the magic of super() in python 3.X which can be called without arguments inside a class. Here is a question regard this very topic stackoverflow.com/questions/19608134/… that is answered nicely by Martijn.
As far as I can tell, that only applies to the 0-argument form of super. Anyway, I'll stop pestering you (sorry about that!) and post a new question about it. Let's clean up this mess, shall we?
Image
I don't think it's true that super(B).__init__() is calling None.__init__(). See this gist for evidence to the contrary: gist.github.com/njvrzm/358f96c971a2adc329bcc1d860bede2a
Image
Actually I've copied the code from that gist to my answer to this related question.
|
2

The confusion here comes from the fact that (in a class definition context) super() gives a bound super object which then delegates __init__ to its __self_class__, while super(B) creates an unbound super object which, because its __self_class__ is None, does not delegate.

In [41]: class Test(int):
    ...:     def __init__(self):
    ...:         print(super().__self_class__)
    ...:         print(super().__init__)
    ...:         print(super(Test).__self_class__)
    ...:         print(super(Test).__init__)
    ...:

In [42]: Test()
<class '__main__.Test'>
<method-wrapper '__init__' of Test object at 0x10835c9c8>
None
<method-wrapper '__init__' of super object at 0x10835c3c8>

So when you call super(B).__init__(), it creates an unbound super but then immediately calls __init__ on it; that, because of the magic described in the various links in this other answer, binds that unbound super. There are no references to it, so it disappears, but that's what's happening under the hood.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.