classic and property() version of same attribute - yikes!

Skip Montanaro skip at pobox.com
Wed Jul 14 14:38:16 EDT 2004


I just stumbled upon a bug in some group-written code.  We have this sort of
class hierarchy:

    class X(object):
        ...


    class A(X):
        def __init__(...):
            self.attr = 0.0

        def GetAttr(self):
            return self.attr

        def SetAttr(self, a):
            self.attr = a


    class B(A):
        ...


    class C(B):
        def set_attr(self, a):
            ...

        def get_attr(self):
            ...

        attr = property(get_attr, set_attr, None, None)

Each of the four classes is defined in a different module.  The author of A
was not aware that it was a new-style class and coded it like a classic
class.  The author of C (different person) apparently knew it was new-style
and took advantage of that fact to use property(), but didn't notice there
was already an attribute named "attr" two levels up.

This of course all gets to the Zen bit: "Flat is better than nested", and
I'll use that as an argument for flatter hierarchies in the future.
Nonetheless, what would be the effect of such an attribute stomping?  Do I
just have multiple ways to change self.attr, assuming __slots__ hasn't been
declared?  This seems like something perhaps pychecker should notice, but
can it?

Thx,

Skip



More information about the Python-list mailing list