Python Macros

Carlos Ribeiro carribeiro at gmail.com
Wed Oct 6 08:26:30 EDT 2004


On Wed, 6 Oct 2004 10:24:44 +0200, Alex Martelli <aleaxit at yahoo.com> wrote:
> 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.

Thanks (again!). Another dumb mistake. I have mixed up __get__attr,
descriptors, and __getattribute__ in my head, all at once, and messed
up the concepts. Too much reading and too little practice do it :-).
The funny thing is that the code worked as written, at least for my
small test case, because it failed to find the dog.fetch method
anyway.
 
> 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)


-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list