should property() fail if not used on new style classes?

Raymond Hettinger vze4rx4y at
Sat Aug 9 01:50:23 CEST 2003

"Kevin Altis"
> Now here is the same example, but the C class no longer subclasses object.
> >>> class C:
> ...     def getx(self): return self.__x
> ...     def setx(self, value): self.__x = value * 2
> ...     x = property(getx, setx)
> ...
> >>> b = C()

Before going further, try this:

     print b.x

You'll see that the property() assignment *did* work.

> >>> b.x = 5
> >>> b.x
> 5
> If getx and setx aren't going to get bound shouldn't property fail in a more
> visible way? Is something else going on that I'm missing?!

What is missing is that there is no property failure.
getx() and setx() *are* bound to the property object
which is in-turn bound to 'x'.  The reason you don't
see it working is that all of the magic occurs in the
__getattribute__ method of new style objects which
do special checking to see if a looked-up object is a

It would be great if old-style classes could also check
looked-up values to see if they were descriptors and issue
an appropriate warning.  Unfortunately, I don't see any
way of doing this without unnecessarily impacting
performance and affecting backward compatability.
The root of the problem is that the descriptors can be
dynamically created:

class C:

# Valid statements that can occur after the class definition
def getx(self): return self.__x
def setx(self, value): self.__x = value * 2
C.x = property(getx, setx)

In contrast, it would be much more reasonable to have
old style classes warn about the use of __slots__.

Raymond Hettinger

More information about the Python-list mailing list