property works because QObject has a metaclass that takes care of them. Witness this small variation on @quark's code...:
from PyQt4.QtCore import QObject
def makec(base):
class X( base ):
def __init__(self):
self.__x = 10
def get_x(self):
print 'getting',
return self.__x
def set_x(self, x):
print 'setting', x
self.__x = x
x = property(get_x, set_x)
print 'made class of mcl', type(X), issubclass(type(X), type)
return X
class old: pass
for base in (QObject, old):
X = makec(base)
x = X()
print x.x # Should be 10
x.x = 30
print x.x # Should be 30
running this emits:
made class of mcl <type 'PyQt4.QtCore.pyqtWrapperType'> True
getting 10
setting 30
getting 30
made class of mcl <type 'classobj'> False
getting 10
30
see the difference? In the class that's really a legacy (old-type) class, the one made the second time, metaclass is classobj
(which ISN'T a subclass of type) and properties don't work right (assigning x.x
bypasses the property, and after that getting x.x
doesn't see the property any more either). But in the first case, the Qt case, there's a different metaclass, and it IS a subclass of type (so it's not really correct to say the class "isn't new-style"!), and things therefore DO work correctly.