Python Macros
Alex Martelli
aleaxit at yahoo.com
Wed Oct 6 04:24:44 EDT 2004
Carlos Ribeiro <carribeiro at gmail.com> wrote:
...
> def __getattr__(self, attrname):
> # self.__dict__ contains the local dict of Dog
> message = getattr(self.__dict__, attrname, None)
The only kind of *attributes* you'll find for self.__dict__ are methods
such as keys, items, copy, and so on. Attributes and items are very
different things, of course. I suspect this code is a serious bug,
i.e., the intention is *NOT* to have somedog.copy() return a copy of
somedog.__dict__ and so on. self.__dict__ will never have attrname as a
key at this point, either, or else __getattr__ would never have been
entered in the first place (don't confuse __getattr__ with the rarely
needed, rarely used __getattribute__!!!). In short, I believe this
whole part and the following if/else construct just need to be excised.
If you want to delegate attribute lookup (including method lookup) from
self to self.owner, ALL you need is:
def __getattr__(self, n): return getattr(self.owner, n)
that's it -- nothing more.
Alex
More information about the Python-list
mailing list