Question about accessing class-attributes.

Michele Simionato mis6 at pitt.edu
Mon May 5 07:54:42 EDT 2003


Bjorn Pettersen wrote:

> <snip objections against the concept of superclass>
> > If super had a __superclass__ attribute I could say to the newbie
> > super(A,C).a <=> super(A,C).__superclass__.a <=> B.a 

...<aside: why not super(A,C).__class__?>..

Because super(A,C).__class__ returns the class of the super object,
<type 'super'> not the superclass of A with respect to the MRO of C !

>[..__getattr__ special case..]

> >Still in the typical case there would be equivalence. 
> >
> > This is the typical case I had in mind (I am thinking about 
> > attributes, not about methods)
>
> I'm not sure why you want to make a difference, but ok. Let me change
> your example just a little:

> def superclass(C,S):
> # mro=list(S.__mro__) let's assume we're dealing with objects instead
> mro = list(S.__class__.__mro__)
> return mro[mro.index(C)+1] 

> class T(object): a = 0 
> class A(T): pass
> class B(T): a = 2
> class C(A,B): a = 3
> c = C()

>> assert super(A,C).a is superclass(A,C).a # okay
>> assert super(A,C) is B.a # okay

> print super(C,c).a, superclass(C,c).a # 2 0

> The key word here is _cooperative_. A singular superclass has no
> meaning, you need the entire list of the remaining superclasses, in the
> right order.

Okay, I see your point now, and I give up. 

> > These are the tricky cases I am aware of, where there is no 
> > equivalence (speaking for attributes only):
> [...]

> Could you give definitions instead of examples :-)

> > In my view, one could say that for most uses super(A,C) can 
> > be seen as a proxy to the methods of superclass(A,C)==B, and 

> From above that's false, it is only true with single inheritance and
> special cases of multiple inheritance. In a previous post I defined the
> semantics of super (it would be helpful if you come up with something
> more precise than "in most cases", e.g. listing when not etc.) by
> introducing the concept of a super_context. You can define a working
> version of your superclass in terms of that too:

> # new version, fixing obj/class problem
> def super_context(A1, A2):
> if isinstance(A2, A1):
> mro = list(A2.__class__.__mro__)
> else:
> mro = list(A2.__mro__)
> remaining = mro[mro.index(A1)+1:]
> return remaining

> class superclass(object):
> def __init__(self, cls, obj):
> self.__ctxt__ = super_context(cls, obj)

> def __getattr__(self, attr):
> for cls in self.__ctxt__:
> if attr in cls.__dict__:
> return cls.__dict__[attr]
> raise AttributeError(attr) 

> whith this definition you can also do:

> superclass(Cls, obj).__getitem__(obj, key)

Right. .If you look at the implementation of Super I posted some time
ago (in another thread, I think), you will see that  it is not very
dissimilar. This is a case were I had a working code but a wrong (or
at least, useless) concept of superclass in mind: you see the
difference between  theory and practice! ;)

> [..pseudocode vs working implementation..]
>
> > We have different philosophies, then. Whereas I am a 
> > theoretical physicists and I have also worked in 
> > mathematical physics and pretty abstract stuff, when 
> > it comes to programming I am an eminently *practical*
> > person.
> [...]

> But this isn't programming. This is semantic analysis which should be
> very similar to theoretical physics (do you need a working experiment to
> discuss a theory?)

I am not familiar with "semantic analysis" (I  can read the BNF and
write down
the simplest production rules, but that' all). It seems to me quite
different from
theoretical physics and the branches of mathematics I am familiar
with.
And even in theoretical physics, you cannot believe *any* theory
without experiments. Strictly speaking, you cannot believe even when
you have
 the experiments ;). You can formulate the theory without experiments,
that's true,
but you are never sure that the theory you formulated is fully
consistent, even at
the pure mathematical level (I have worked on these issues during my
Ph.D. ,
therefore I speak for experience). 
The only thing you can say is that some theories are so obviously
stupid that
you don't need any experiment to refute them ;)

> -- bjorn

Cheers,
          Michele




More information about the Python-list mailing list