super() doesn't get superclass

Evan Klitzke evan at yelp.com
Tue Sep 18 00:42:41 EDT 2007


On Tue, 2007-09-18 at 14:15 +1000, Ben Finney wrote:
> Howdy all,
> 
> After banging my head against super() trying to reliably get
> attributes of a superclass, I gained a little enlightenment when this
> turned up in a search:
> 
>     "Python's Super is nifty, but you can't use it
>     (Previously: Python's Super Considered Harmful)"
>     <URL:http://fuhm.org/super-harmful/>
> 
> An early paragraph stood out that made my current problems clear:
> 
>     One big problem with 'super' is that it sounds like it will cause
>     the superclass's copy of the method to be called. This is simply
>     not the case, it causes the next method in the MRO to be called.
> 
> Oh. The author's right, I got that mistaken impression too. Obviously,
> then, I must have misread the documentation of 'super'. I went back to
> double-check, and was appalled to find:
> 
>     super(type[, object-or-type])
>     Return the superclass of type. [...]
> 
>     <URL:http://docs.python.org/lib/built-in-funcs.html#l2h-72>
> 
> Why does the documentation of 'super' say that it returns the
> superclass when *that's not true*? It doesn't return the superclass,
> it returns the next class in the MRO, whether that's a superclass or
> not.

The next class in the MRO _is_ a superclass. Maybe not the way you've
defined it in your own code, but certainly of the new class you created
with MI.

> Actually, even that's not true. The error message "AttributeError:
> 'super' object has no attribute 'bazfunc'" makes it clear that 'super'
> actually returns not the superclass, but a 'super' object, whatever
> that's supposed to be.

No, super really does return a class. What makes you think that super
returns a special sort of object?

> After reading the rest of the article, I'm amazed that 'super' as
> currently implemented is in Python at all. I agree with the author
> that it's useless unless *everyone* uses it for *everything*, and even
> then it's pretty limited.

I don't see what the problem is. If you don't use super, then you can
break the inheritance chain, but this is no different than calling
superclass methods by passing in self (i.e. the old-style way of doing
it), and in fact using super is far more flexible. There's no way to
magically know when to call the superclass' method, and even if you knew
when, there's no way to magically know when it needs to be called.

If you're using multiple inheritance, and you're _not_ using super
everywhere, then your code is broken anyway. Use super correctly in your
own code, and you don't need to worry about other people using it
incorrectly.

-- 
Evan Klitzke <evan at yelp.com>




More information about the Python-list mailing list