Implied parameters for super method in python

  multiple-inheritance, python

In a course about multiple-inheritance I came across this beautiful piece of code.

class First(object):
    def __init__(self):
        super(First, self).__init__()
        print("first")

class Second(object):
    def __init__(self):
        super(Second, self).__init__()
        print("second")

class Third(Second,First):
    def __init__(self):
        super(Third, self).__init__()
        print("third")

Third();

output:

first
second
third

I was a bit daunted so I started investigating why this stuff even works. I found a lovely article about this matter that explained some stuff about the mechanics behind super pretty well.

Then I started playing with the code and I found out that most of the code is actually redundant. I trimmed stuff step by step until only this poor skeleton remained.

class First():
    def __init__(self):
        super().__init__()
        print("first")

class Second():
    def __init__(self):
        super().__init__()
        print("second")

class Third(Second,First):
    def __init__(self):
        super().__init__()
        print("third")

Third();

This produces the exact same output. Now for object I understand that this can be left out, because all classes derive from the base class object. No problems there. However I don’t quite understand what’s behind the arguments that are passed to the super method. So I made this final adjustment to check if this would work as well:

class First():
    def __init__(self):
        super().__init__()
        print("first")

class Second():
    def __init__(self):
        super().__init__()
        print("second")

class Third(Second,First):
    def __init__(self):
        super(self).__init__()
        print("third")

Third();

output:

TypeError: super() argument 1 must be type, not Third

I can understand that self refers to Third in this case. In the case of super(Third,self), Third refers to a class which then would suffice as a type I suppose. So is it correct that when calling the super method, all of these redundant parts are always implied? Does anyone have some cool documentation about these mechanics other than the link I already found myself? It seems also that the order in which they are called is the reverse order in which the class arguments are listed.
reversing into class Third(First,Second) gives output:

second
first
third

Some additional light on the matter would be much appreciated.

Source: Python Questions

LEAVE A COMMENT