[Python-Dev] Change in evaluation order in new object model

Thomas Heller thomas.heller@ion-tof.com
Fri, 2 Nov 2001 19:07:16 +0100


From: "M.-A. Lemburg" <mal@lemburg.com>
> Michael McLay wrote:
> > 
> > I was suprised by a change to the order of evaluation of members in the new
> > object type.  I haven't found an explanation for why the change was made.  I
> > was comfortable with the way it worked.  Is there an advantage to the change?.
> > 
> > In the classic python model the interpreter looked in the instance dictionary
> > and if the name wasn't there it looked in the class dictionary.  The
> > following illustrates this evaluation order.
> > ...
> > 
> > With the new slots mechanism the order has been reversed.  The class level
> > dictionary is searched and then the slots are evaluated.
> > 
> > >>> class B(object):
> >     __slots__ = ['a','b','c']
> > 
> > >>> b = B()
> > >>> b.a = 4
> > >>> b.a
> > 4
> > >>> B.a = 6
> > >>> b.a
> > 6
> > >>> b.a = 8
> > Traceback (most recent call last):
> >   File "<pyshell#61>", line 1, in ?
> >     b.a = 8
> > AttributeError: 'B' object attribute 'a' is read-only
> 
> Could someone please first explain what these slots are used for 
> in the first place :-? There must be some difference to standard
> class attributes... which is probably also the reason for the
> above behaviour (even though it does look like a bug to me).

The lookup order (instance dict, then class dict) hasn't changed,
just there is npo instance dict any longer.

B.a is an attribute descriptor which retrieves the 'a' attribute
from the instance. B.a = 6 doesn't change anything in the instance,
it just masks the slot so that it isn't accessible any longer.

Is this a bug, a feature or an implementation detail? No idea...

Thomas