class fields vs __getattr__
Dave Cole
djc at object-craft.com.au
Thu Mar 8 20:06:18 EST 2001
>>>>> "Joshua" == Joshua Marshall <jmarshal at mathworks.com> writes:
Joshua> If you try to get a field of an object, it seems __getattr__
Joshua> is called only if no such field exists on the class. For
Joshua> exmaple:
Joshua> >>> class T:
Joshua> ... x = 1
Joshua> ... def __getattr__(self, name):
Joshua> ... return 2
Joshua> ...
Joshua> >>> ob = T()
Joshua> >>> ob.foo
Joshua> 2
Joshua> >>> ob.x
Joshua> 1
Joshua> I would have expected ob.x to return 2. Can anyone explain
Joshua> the rationale behind this behavior?
__getattr__ is only used when Python cannot find the named attribute.
- Dave
--
http://www.object-craft.com.au
More information about the Python-list
mailing list