[Edu-sig] Attribute Lookup Order

Scott David Daniels Scott.Daniels at Acm.Org
Fri Feb 6 20:54:19 CET 2009


About my message:
 >> ...  Nickel summary, lookup order is not dirt simple, and reading
 >> and writing work differently.

David MacQuigg wrote:
> Maybe I'm missing some subtleties, but it still seems simple to me.
 > An attribute is looked up in the order object -> class -> superclasses,
 > *UNLESS* that attribute happens to be a property of the class....
The subtleties involve the difference in the lookup order between
read-only properties and properties with a write method.

> ...
> class Rectangle(object):
>     def __init__(self, width, height):
>         self.width = width
>         self.height = height
> #        self.area = width * height
> 
>     def getArea(self):
>         return self.width * self.height
> ...

Written in later versions of Python (2.5 and up) as:
     class Rectangle(object):
         def __init__(self, width, height):
             self.width = width
             self.height = height
             # self.area = width * height

         @property
         def area(self):
             return self.width * self.height


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Edu-sig mailing list