Builtn super() function. How to use it with multiple inheritance? And why should I use it at all?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Jul 31 06:22:40 EDT 2010


On Fri, 30 Jul 2010 21:40:21 -0600, Ian Kelly wrote:

> I have to chime in and agree that the name "super" is problematic. I'm
> reading this thread with a sense of alarm because I apparently never
> read the super() documentation too closely (why would I?  "Oh, it just
> accesses an attribute from a superclass.  Moving on.") and have been
> writing code for the past four years under the impression that super()
> will always refer to a superclass of the current class. 

In Python 2.x, super() doesn't know what the current class is. You have 
to explicitly tell it. If you tell it a lie, surprising things will 
happen.

Assuming you accurately tell it the current class, can you give an 
example where super() doesn't refer to a superclass of the current class?



[...]
> On a tangent, is it just me, or is the super() documentation incorrect,
> or at least unclear?  Quoting from the first two paragraphs:

Yes, it's a bit unclear, because it's a complex function for dealing with 
a complicated situation. But if you have single inheritance, it's simple. 
Anywhere you would write 

class C(B):
    def method(self, *args):
        B.method(*args)

you can write 

class C(B):
    def method(self, *args):
        super(C, self).method(*args)


and it will Just Work.



> super(type[, object-or-type])
> 
>     Return a proxy object that delegates method calls to a parent or
> sibling class of type. 

I think that the bit about sibling class refers to super(type, type2) 
calls, rather than the super(type, instance) calls which I've been 
discussing. I've never needed, and don't understand, the two type version 
of super(), so I can't comment on it.


-- 
Steven



More information about the Python-list mailing list