general class functions

Kent Johnson kent3737 at yahoo.com
Tue Nov 2 13:31:42 EST 2004


syd wrote:
> Many many thanks, Andrea and Kent!
> 
> To keep changes to calls in my current code to a minimum, I'm hoping I
> can encode Andrea's "on-the-fly" get function.  I added Andrea's
> __getattr__ to my Library() class, populated the class with a few
> nations, and tried:
> 
> 
>>>>library.get_continent('Europe')
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "foo.py", line 16, in __getattr__
>     return self.__dict__[name]
> KeyError: '__repr__'

Instead of
   return self.__dict__[name]
you need
   raise AttributeError, name

self.__dict__ has already been tried before __getattr__ is called, you 
don't need to try it again.

Kent



More information about the Python-list mailing list