super() doesn't get superclass
Ben Finney
bignose+hates-spam at benfinney.id.au
Tue Sep 18 01:24:21 EDT 2007
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"? No, because they're
entirely unrelated except that they inherit from 'object'. The
superclass of 'A' is 'object'.
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'.
You seem to be saying that now suddenly D *is* a superclass of
A. That's certainly not what users will think of when they think
"superclass" though.
> > 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?
>>> super(A)
<super: <class 'A'>, NULL>
>>> super(A).__class__
<type 'super'>
What I expect, incidentally, is that a function documented as "returns
the superclass of 'type'" would actually return that class (in this
case, the type 'object'), not an object of 'super' type.
> > 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
I don't want to break the inheritance chain. I want the superclass,
not the "next class in the MRO".
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'".
Am I mistaken in thinking that "superclass of foo" is equivalent to
"parent class of foo"? If so, I'd lay heavy odds that I'm not alone in
that thinking.
--
\ "Laugh and the world laughs with you; snore and you sleep |
`\ alone." —anonymous |
_o__) |
Ben Finney
More information about the Python-list
mailing list