Super Python For S60v3

  
Super Python For S60v3 Rating: 3,8/5 1084reviews

I am new in Python, so I still have confusions about object-oriented programming, and now a days I am getting more confused about the 'super' function.

I'm running Python 2.5, so this question may not apply to Python 3. When you make a diamond class hierarchy using multiple inheritance and create an object of the derived-most class, Python does the Right Thing (TM). It calls the constructor for the derived-most class, then its parent classes as listed from left to right, then the grandparent.

I'm familiar with Python's; that's not my question. I'm curious how the object returned from super actually manages to communicate to calls of super in the parent classes the correct order. Consider this example code: #!/usr/bin/python class A(object): def __init__(self): print 'A init' class B(A): def __init__(self): print 'B init' super(B, self).__init__() class C(A): def __init__(self): print 'C init' super(C, self).__init__() class D(B, C): def __init__(self): print 'D init' super(D, self).__init__() x = D() The code does the intuitive thing, it prints: D init B init C init A init However, if you comment out the call to super in B's init function, neither A nor C's init function is called. This means B's call to super is somehow aware of C's existence in the overall class hierarchy. I know that super returns a proxy object with an overloaded get operator, but how does the object returned by super in D's init definition communicate the existence of C to the object returned by super in B's init definition? Is the information that subsequent calls of super use stored on the object itself? If so, why isn't super instead self.super?

Super Python For S60v3

In Python, super() built-in has two major use cases: Allows us to avoid using base class explicitly; Working with Multiple Inheritance.

Super Python For S60v3

Edit: Jekke quite rightly pointed out that it's not self.super because super is an attribute of the class, not an instance of the class. Conceptually this makes sense, but in practice super isn't an attribute of the class either! You can test this in the interpreter by making two classes A and B, where B inherits from A, and calling dir(B). It has no super or __super__ attributes. Formula 1 2014 Game Pc Tpb on this page. EDIT: Reading Jacob Gabrielson's link to has exposed my Java background - super() definitely behaves differently in Python.

I'll leave the rest of my answer for reference below: Posting as an answer, because it's too big for a comment: Now that I look at it again, and keep in mind I don't have an interpreter handy, wouldn't you expect to see: D init B init C init A init anyway, because C will be calling super() regardless? At any rate, I believe the behaviour can probably be explained by the fact that everyone should be aware that D i – Mar 3 '09 at 17:16. I have provided a bunch of links below, that answer your question in more detail and more precisely than I can ever hope to. I will however give an answer to your question in my own words as well, to save you some time.

I'll put it in points - • super is a builtin function, not an attribute. • Every type (class) in Python has an __mro__ attribute, that stores the method resolution order of that particular instance. • Each call to super is of the form super(type[, object-or-type]). Let us assume that the second attribute is an object for the moment. • At the starting point of super calls, the object is of the type of the Derived class ( say DC). • super looks for methods that match (in your case __init__) in the classes in the MRO, after the class specified as the first argument (in this case classes after DC).