Question about accessing class-attributes.

Michele Simionato mis6 at pitt.edu
Thu May 1 13:17:16 EDT 2003


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>
> > Certainly the documentation in
> > http://www.python.org/dev/doc/devel/lib/built-in-funcs.html
> > 
> > has to be changed. It says:
> > 
> > """
> >  super(type[object-or-type])
> > 
> >       Return the superclass of type. If the second argument is omitted
> > the super object returned is unbound. If the second argument is an
> > object, isinstance(obj, type) must be true. If the second argument is
> > a type, issubclass(type2, type)
> > must be true. """
> > 
> > But it DOES not return the superclass! I admit I myself at the
> > beginning was confused.
> 
> The documentation is surely imperfect (e.g. by making no reference to
> the mro, which is crucial in the case in which the 2nd argument is
> there and is an instance of the first one).  But doesn't the second
> sentence say explicitly that e.g. super(C) returns a "super object"
> that is unbound? 

Yes, but the first sentence says it returns the superclass, therefore from
the second sentence one could infer that a super object is a class (??).
It is confusing, at least.

> Maybe by "superclass" you mean "base", a common
> usage, but then the singular "the superclass" would be absurd.

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




More information about the Python-list mailing list