18

Considering:

class Parent(object):

    def altered(self):
        print "PARENT altered()"

class Child(Parent):

    def altered(self):
        print "CHILD, BEFORE PARENT altered()"
        super(Child, self).altered()    # what are the arguments needed?  Why Child and self?
        print "CHILD, AFTER PARENT altered()"

In Python 2.7, Why must Child be passed as an argument to the super() call? What are the exact intricacies of using super instead of just letting it work.

2
  • 7
    As far as I know, it has to do with the MRO. But I'm not good enough at explaining it for a "real" answer. Maybe this helps: rhettinger.wordpress.com/2011/05/26/super-considered-super Note though that the arguments aren't needed anymore in Python 3. Commented Apr 9, 2013 at 7:54
  • So the first argument being "Child" is for the class you are in and not the one you want to inherit from since Parent is the base? Something like that I suppose. Also thanks for the read and I am using Python 2.7 to be clear. I will keep reading about super() Commented Apr 9, 2013 at 7:59

2 Answers 2

25

super figures out which is the next class in the Method Resolution Order. The two arguments you pass in are what lets it figure that out - self gives it the entire MRO via an attribute; the current class tells it where you are along the MRO right now. So what super is actually doing is basically:

def super(cls, inst):
    mro = inst.__class__.mro() # Always the most derived class
    return mro[mro.index(cls) + 1]

The reason it is the current class rather than the base class is because the entire point of having super is to have a function that works out what that base class is rather than having to refer to it explicitly - which can cause problems if the base class' name changes, if you don't know exactly what the parent class is called (think of factory functions like namedtuple that spit out a new class), and especially in multi-inheritance situations (where the next class in the MRO mightn't be one of the current class' bases).

Sign up to request clarification or add additional context in comments.

4 Comments

The reason it is the current class rather than the base class is because the entire point of having super is to have a function that works out what that base class is rather than having to refer to it explicitly. I think that line you stated summed it up nicely. I will look into the MRO and realize that this is just a facet of python 2.x since python 3 doesn't require these arguments. Thank you.
@klandshome Python 3's super works exactly the same way - it just has some interesting augmentations to be able to figure out the arguments for itself in most circumstances (by inspecting the call stack) rather than you having to pass them in.
Hmm. Great information. The call stack. I learned a great deal. Thank you.
This is an extraordinary explanation!
0

You don't have to pass the child instance as an argument from python3 onwards if I'm not mistaken. Plus for an in-depth understanding of the super() method refer to https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.