[ZODB-Dev] ZODB 3.2.4 strange getattr, hasattr behaviour

Paul Winkler pw_lists at slinkp.com
Tue Nov 23 12:11:09 EST 2004


On Tue, Nov 23, 2004 at 01:01:36PM +0100, Syver Enstad wrote:
> Syver Enstad <syver at inout.no> writes:
> 
> > I get None when I do getattr on a not existing attribute in a
> > Persistent derived class (instead of AttributeError) . This doesn't
> > happen all the time but occurs in an upgrade script. hasattr for the
> > same attribute also returns True, but it is not in self.__dict__ and
> > delattr raises KeyError.
> > 
> > I will investigate further and I see what I find out. Maybe it is
> > something strange in my code which causes this behaviour.
> 
> Aargh, fooled by class level default attribute. Thanks for watching me
> make a fool of myself :-). Add one more occurence of being fooled due
> to python's way of looking up attributes in the instance and then in
> the class. Did someone say explicit is better than implicit ;-)

Yes, but also practicality beats purity ;-)
This is a very handy feature, I use it all the time.
E.g. for attributes whose values are immutable types, it's a 
useful upgrade mechanism for persistent instances.

-- 

Paul Winkler
http://www.slinkp.com


More information about the ZODB-Dev mailing list