[Edu-sig] Attribute Lookup Order
David MacQuigg
macquigg at ece.arizona.edu
Sat Feb 7 21:49:11 CET 2009
At 11:54 AM 2/6/2009 -0800, Scott David Daniels wrote:
>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
>>
>> area = property(getArea)
>>...
>
>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
Nice! Syntax sugar for the more explicit form. I'll update my examples.
As for the subtle changes in lookup order, I could include some discussion under Advanced Topics (Metaclasses, Cooperative Super Calls, etc.), but I will need to see a clear example that is relevant to the non-programmer scientists and engineers I am targeting. Else, the value is too low and the complexity too high.
The @property example above *is* appropriate for my OOP chapter (under the topic Robust Programming). It is simple, and understanding the concept of protected attributes is good even if you don't use them.
-- Dave
More information about the Edu-sig
mailing list