Hay un programa simple en python3:python3 súper no funciona con las clases de PyQt
from PyQt4 import QtCore
import PyQt4
class Bar(object):
def __init__(self):
print("Bar start")
super(Bar, self).__init__()
print("Bar end")
class FakeQObject(object):
def __init__(self):
print("FakeQObject start")
super(FakeQObject, self).__init__()
print("FakeQObject end")
class Foo(QtCore.QObject, Bar):
#class Foo(FakeQObject, Bar):
def __init__(self):
print("Foo start")
super(Foo, self).__init__()
print("Foo end")
print(Foo.__mro__)
print(PyQt4.QtCore.PYQT_VERSION_STR)
f = Foo()
a) Cuando la clase Foo hereda de QtCore.QObject y bar obtenemos:
(<class '__main__.Foo'>, <class 'PyQt4.QtCore.QObject'>, <class 'sip.wrapper'>, <class 'sip.simplewrapper'>, <class '__main__.Bar'>, <class 'object'>)
4.9.4
Foo start
Foo end
b) Cuando la clase Foo hereda de FakeQObject y bar obtenemos:
(<class '__main__.Foo'>, <class '__main__.FakeQObject'>, <class '__main__.Bar'>, <class 'object'>)
4.9.4
Foo start
FakeQObject start
Bar start
Bar end
FakeQObject end
Foo end
la pregunta es: ¿por qué en el caso a), Bar init no se llama?
He encontrado una pregunta similar aquí pyQt4 and inheritance pero no hay buenas respuestas.
¡Gracias de antemano!
Sospecho que es porque 'QtCore.QObject' no utiliza la cooperativa super'__init__'. Por cierto, en Python 3, no necesitas 'super (Foo, self)'; 'super()' debería ser suficiente. – nneonneo
Sí, pero el sitio de PyQt http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/gotchas.html dice: "En las versiones de PyQt anteriores a la v4.5, había restricciones en el uso de super con Clases de PyQt. Estas restricciones ya no se aplican con v4.5 y posterior ". Entonces, este es un error de PyQt, ¿no es así? – vitvlkv
Gracias por super() sin params, no lo sabía .. – vitvlkv