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

Tim Peters tim at zope.com
Tue Nov 23 15:04:54 EST 2004


[Syver Enstad]
> 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 ;-)

Well, that's pretty fundamental to the way old-style classes work.  If you
have:

    class A:
        ...
        def some_method(self):
            ...
        ...

    a = A()
    a.some_method()

that's how ".some_method" gets resolved:  it looks first in a.__dict__, then
failing to find "some_method" there, looks in A.__dict__.  Most data
attributes work the same way (with exceptions; e.g., when doing a.__class__,
"__class__" isn't looked up in anyone's __dict__, and if you create a value
for "__class__" in a.__dict__ it will be ignored; __class__ is special).

The question I have is about "add one more occurrence ...":  if this is
something that routinely bites you, why do you create class data attributes?
Unlike class method attributes, class data attributes are almost never
necessary.  IOW, if punching yourself in the eye hurts, there are
alternatives to wishing your knuckles were softer.  For example, you could
wish that your eye were harder <wink>.



More information about the ZODB-Dev mailing list