class fields vs __getattr__

Alex Martelli aleaxit at yahoo.com
Fri Mar 9 05:58:02 EST 2001


"Joshua Marshall" <jmarshal at mathworks.com> wrote in message
news:9898al$j3j$1 at news.mathworks.com...
> If you try to get a field of an object, it seems __getattr__ is called
> only if no such field exists on the class.  For exmaple:

Yes, this is the defined semantics of __getattr_: it's designed
and documented that way.

> I would have expected ob.x to return 2.  Can anyone explain the
> rationale behind this behavior?

Performance AND simplicity in coding your __getattr__ -- when
called it KNOWS the attribute being requested is not already
defined (in the instance, its class, its bases).


Alex






More information about the Python-list mailing list