python3 super doesn't work with PyQt classes
There is a simple program in python3:
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) When class Foo inherits from QtCore.QObject and Bar we get:
(<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) When class Foo inherits from FakeQObject and Bar we get:
(<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
The question is: why in the a) case, Bar init is not called?
I found similar question here pyQt4 and inheritance but there are no good answers.
Thanks in advance!
Along with @nneonneo I also suspect that QtCore.QObject
doesn't use the cooperative super.__init__
. If it did, you wouldn't have this problem.
However, you should be aware that at some point one of the base classes cannot use cooperative super because object
won't have the method. Consider:
class Base():
def __init__(self):
print("initializing Base")
super().__init__()
def helper(self, text):
print("Base helper")
text = super().helper(text)
text = text.title()
print(text)
class EndOfTheLine():
def __init__(self):
print("initializing EOTL")
super().__init__()
def helper(self, text):
print("EOTL helper")
text = super().helper(text)
return reversed(text)
class FurtherDown(Base, EndOfTheLine):
def __init__(self):
print("initializing FD")
super().__init__()
def helper(self, text):
print(super().helper(text))
test = FurtherDown()
print(test.helper('test 1 2 3... test 1 2 3'))
and the output:
initializing FD
initializing Base
initializing EOTL
Base helper
EOTL helper
Traceback (most recent call last):
File "test.py", line 28, in <module>
print(test.helper('test 1 2 3... test 1 2 3'))
File "test.py", line 25, in helper
print(super().helper(text))
File "test.py", line 7, in helper
text = super().helper(text)
File "test.py", line 17, in helper
text = super().helper(text)
AttributeError: 'super' object has no attribute 'helper'
So, whichever class is going to be the End of the Line needs to not call super
. Because there are other Qt
methods you might want to override, that dictates that the Qt
class must be the last one in the class header. By not having __init__
use cooperative super, even though it could, Qt
is avoiding bugs further down when some other method is overridden.
上一篇: 有没有更聪明的方法来禁用Symfony2.1中的Doctrine2过滤器?
下一篇: python3超级不适用于PyQt类