Rewriting __getattr__

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Jan 7 08:45:56 EST 2011


kost BebiX wrote:
> Hi everyone!
> I just saw a bug (?) in bson.dbref:DBRef.__getattr__
>
> Here's they're code:
>     def __getattr__(self, key):
>         return self.__kwargs[key]
>
> And when you do copy.deepcopy on that object it will raise you KeyError. So here's a small piece of code that reproduces the problem:
>
>   
from http://docs.python.org/reference/datamodel.html

About __getattr__
"This method should return the (computed) attribute value or raise an 
AttributeError 
<http://docs.python.org/library/exceptions.html#exceptions.AttributeError> 
exception."

The code you provided raises a KeyError thus methods such as 'getattr' 
will fail as they expect an AttributeError exception.

JM



More information about the Python-list mailing list