super() doesn't get superclass

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Sep 18 10:12:07 EDT 2007


Ben Finney a écrit :
> Evan Klitzke <evan at yelp.com> writes:
> 
>> On Tue, 2007-09-18 at 14:15 +1000, Ben Finney wrote:
>>> 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.
> 
> If I define a class hierarchy as follows::
> 
>     class A(object): pass
>     class B(object): pass
>     class C(A): pass
>     class D(B): pass
> 
> is it true to say that "D is a superclass of A"? 

Obviously not.

> How about this:
> 
>     class E(C, D): pass
> 
> In this instance, the MRO now has D following A; but the superclass of
> 'A' is still 'object'.

Yes. And ?

> 
> You seem to be saying that now suddenly D *is* a superclass of
> A. 

I don't see such an assertion in Evan's answer. Chapter and verse ???

> That's certainly not what users will think of when they think
> "superclass" though.

If a class X is in the MRO of call Y, then X is a superclass of Y. I 
agree that the documentation for super is somewhat misleading (and 
obviously wrong), but it still *give access to* (at least one of) the 
superclass(es).

>>> 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.

The 'super' object is a wrapper around a class and an object. It 
delegates attribute lookups to appropriate superclass.

>>> After reading the rest of the article, I'm amazed that 'super' as
>>> currently implemented is in Python at all.

If you have a better solution for handling multiple inheritence, please 
share with us.

FWIW, after all the articles I've read explaining why Python is badly 
designed, badly implemented, and totally flawed, I do wonder why this 
language exists at all !-)

> I don't want to break the inheritance chain. I want the superclass,

A soon as you have either multiple inheritence and/or an inheritence 
tree with depth > 1, there's no such thing as "the" superclass. wrt/ 
your exemple, object, A, B, C and D are *all* superclasses of E.

> not the "next class in the MRO".

The next class in the MRO is (usually and AFAICT) the most direct 
superclass.

> Orthogonally to that, I think it's madness to write a function for
> "return the next class in the MRO for 'type'" and document it as
> "return the superclass of 'type'".

I wouldn't use such an extreme word as 'madness', but I totally agree 
that this should be corrected. Care to submit a doc patch ?




More information about the Python-list mailing list