Question about accessing class-attributes.

Alex Martelli aleax at aleax.it
Thu May 1 19:00:07 EDT 2003


Michele Simionato wrote:

> Alex Martelli <aleax at aleax.it> wrote in message
> news:<joYra.44343$K35.1283700 at news2.tin.it>...
>> Michele Simionato wrote:
> 
>> > I would like to have an additional attribute (not a method)
>> > returning the superclass. <snip>
   ...
> Given a class C and a subclass S, there is ONLY ONE superclass of C
> with respect to the MRO of S. I have a routine returning the list of

Well yes, but how does class C's attribute (not a method) get the
slightest idea about using the MRO of S rather than the MRO of any
other subclass of C?

> the ancestors of A with respect to the MRO of S:
> 
>   def ancestor(C,S=None):
>       """Returns the ancestors of the first argument with respect to the
>       MRO of the second argument. If the second argument is None, then
>       returns the MRO of the first argument."""
>       if C is object:
>           raise TypeError("There is no superclass of object")
>       elif S is None or S is C:
>           return list(C.__mro__)
>       elif issubclass(S,C): # typical case
>           mro=list(S.__mro__)
>           return mro[mro.index(C):] # compute the ancestors from the MRO
>           of S
>       else:
>           raise TypeError("S must be a subclass of C")
> 
> The superclass is the first ancestor, ancestor(C,S)[1].

Maybe, given S, but you asked for *AN ATTRIBUTE OF C*.  So
would that attribute be identically equal to C.__bases__[0]?
WHY???

> I would like super to have a reference to it. Useful at least
> for pedagogical purposes, if you want to understand the methods
> of which class super will be a proxy to.

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





More information about the Python-list mailing list