Does property in an old-style python class use problems?

Pretty simple question. I saw that in many places he mentioned that using properties in an old-style class should not work, but apparently the Qt classes (via PyQt4) are not new, and some of them have properties in the code I am working with ( and as far as I know, the code does not show any problems)

I came across pyqtProperty function, but I can not find documentation about this. Would this be a good alternative in this case?

+5
source share
3 answers

the property works because QObject has a metaclass that takes care of them. Witness this little variation on @quark 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

:

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

? , ( ), , - classobj ( ), ( x.x , x.x ). , Qt, , ( , " "!), DO .

+4

Python, , PyQt4. , PyQt4 , - , , . PyQt 4.4 Python 2.5:

from PyQt4.QtCore import QObject

class X( QObject ):

    def __init__(self):
        self.__x = 10

    def get_x(self):
        return self.__x

    def set_x(self, x):
        self.__x = x

    x = property(get_x, set_x)

x = X()
print x.x # Should be 10
x.x = 30
print x.x # Should be 30

pyqtProperty Qt, Python. Qt Qt ++ ( Python ) Qt , Qt Designer Qt Creator IDE. , Python, ++. , Qt ++, , PyQt , ( , , / ..). , , - , - . Python Qt, Python , Qt. - PyQt ++ Qt, Qt- Python.

+3

, PyQt4.5 Qt, , , :

from PyQt4 import QtGui
print QtGui.QWidget.__mro__
(<class 'PyQt4.QtGui.QWidget'>, <class 'PyQt4.QtCore.QObject'>, <type 'sip.wrapper'>, <class 'PyQt4.QtGui.QPaintDevice'>, <type 'sip.simplewrapper'>, <type 'object'>)
+1

All Articles