Question about accessing class-attributes.
Michele Simionato
mis6 at pitt.edu
Fri May 2 10:48:47 EDT 2003
Alex Martelli <aleax at aleax.it> wrote in message news:<XFhsa.30684$3M4.874686 at news1.tin.it>...
> <snip various objections not related to what I am trying to say>
> I don't see any pedagogical utility in claiming there is one
> distinguished superclass. The super will quite happily be a
> proxy to methods in other and completely unrelated classes if
> the methods don't happen to exist directly in the one and
> only one you want to present as "THE" superclass. Therefore,
> it seems to me that said hypothetical class attribute would
> only breed confusion and resulting errors.
>
>
> Alex
Clearly, we are having some misunderstanding ( I don't know at
which level). I do think the concept of superclass (even in a multiple
inheritance situation) is well defined, useful and not confusing at all.
Let me try to explain.
Consider your example:
class A(object): a = 23
class B(object): a = 45
class C(A, B): a = 67
which is the typical diamond diagram:
object
/ \
A B
\ /
C
Now we want to explain to the newbie what super(A, C).a returns.
To this aim, we look at the MRO of C, which is the list [C,A,B,object].
Then, we define the superclass of A with respect to the MRO of C,
as the class following A in the list [C,A,B,object]. In this case the
superclass is B. Then we say to the newbie that super(A,C).a is *ideally*
equivalent to B.a and therefore it returns 45.
If super had a __superclass__ attribute I could say to the newbie
super(A,C).a <=> super(A,C).__superclass__.a <=> B.a
Of course, the equivalence would not be exact in subtle cases,
i.e. when B does not have an 'a' attribute, nor it inherits it,
but it has (or inherits) a __getattr__ defining 'a' (as Bjorn
pointed out)
Still in the typical case there would be equivalence.
I made my point clear, now ? At least, this is the way I followed
to teach to myself how super works.
Ciao,
Michele
More information about the Python-list
mailing list