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

Michael McLay mclay@nist.gov
Thu, 1 Nov 2001 18:43:17 -0400


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.

>>> class C:
	def __init__(self):
		self.a = 4
		
>>> c = C()
>>> c.a
4
>>> C.a = 6
>>> c.a
4
>>> c.a = 8
>>> c.a
8
>>> 

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