954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Super declerative inside base class?

In one of the books I'm using to study Python I keep seeing things like:

class Application(Frame):
           super(Application, self).__init__(master):
           self.grid()
           self.bttn_clicks=0
           self.create_widget"""which is another function in the class"""

Why is the super necessary for the class if the class it's calling is itself?

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

to expand on why I'm confused let me explain my understanding of super()

class Classy(Superclass):
         super(Superclass).__init__(self):
            self.something()

that^ makes sense to me, however when the class it is calling, or if Classy's __init__ had super(Classy).init__(self), just does not make sense to me...

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

Consider the following class hierarchy

import inspect

class A(object):
    def func(self):
        return "A.func"

class B(A):
    def func(self):
        return "B.func"
    
class C(A):
    def func(self):
        return "C.func"
    
class D(B, C):
    def func(self):
        return "D.func"
    
class E(D):
    def func(self):
        return "E.func"
    
if __name__ == "__main__":
    def printmro(k):
        print "mro({0}): {1}".format(k.__name__,
                ", ".join(klass.__name__ for klass in inspect.getmro(k)))
    for k in (A,B,C,D,E):
        printmro(k)
    x = E()
    print "type(x):", type(x).__name__
    print "x.func():", x.func()
    print "super(D, x).func():", super(D, x).func()

""" my output-->
mro(A): A, object
mro(B): B, A, object
mro(C): C, A, object
mro(D): D, B, C, A, object
mro(E): E, D, B, C, A, object
type(x): E
x.func(): E.func
super(D, x).func(): B.func
"""

Themro of a type is the 'method resolution order'. It means that when looking for the method x.func, since x is a E instance, the interpreter looks for a method func() in each of the classes E, D, B, C, A, object in this order, because this is class E's mro.

Now, since x is also an instance of D, we can write super(D, x).func(). Then the method will be searched in D's mro without the class D itself. Since D's mro is D, B, C, A, object, python looks in the list of classes B, C, A, object, and it finds B.func().

In python 2, super is not very useful because one must explicitely use a reference to the class (Application in your example). In python 3, you can use super() in a method without the class name and this improves maintenability because the class name can be changed without modifying the code.

Attachments diagram.png 4.89KB
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

That helped a lot, thank you!

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: