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

Kevin Altis altis at semi-retired.com
Fri Aug 8 18:43:42 CEST 2003


While trying to make use of the property function I forgot to subclass from
object which left me quite confused because my get method seemed to be
working, but set wasn't doing what I expected. This makes me wonder whether
the property function should be throwing an exception if it is used on
something other than a new-style class?

  http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-57

First of all, here is a simple variation of the documentation example that
works as expected.

>>> class C(object):
...     def getx(self): return self.__x
...     def setx(self, value): self.__x = value * 2
...     x = property(getx, setx)
...
>>> a = C()
>>> a.x = 5
>>> a.x
10

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()
>>> 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?!

ka





More information about the Python-list mailing list