python3 super不能与PyQt类一起使用

vit*_*lkv 7 pyqt4 method-resolution-order python-3.x

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()
Run Code Online (Sandbox Code Playgroud)

a)当类Foo继承自QtCore.QObject和Bar时,我们得到:

(<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
Run Code Online (Sandbox Code Playgroud)

b)当类Foo继承自FakeQObject和Bar时,我们得到:

(<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
Run Code Online (Sandbox Code Playgroud)

问题是:为什么在a)情况下,Bar init没有被调用?

我在这里找到类似的问题pyQt4和继承,但没有好的答案.

提前致谢!

Eth*_*man 3

与@nneonneo 一起,我也怀疑QtCore.QObject不使用合作社super.__init__。如果是的话,你就不会有这个问题了。

但是,您应该意识到,在某些时候,其中一个基类无法使用合作超级,因为object没有该方法。考虑:

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'))
Run Code Online (Sandbox Code Playgroud)

和输出:

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'
Run Code Online (Sandbox Code Playgroud)

因此,无论哪个类将成为行尾,都不需要调用super. 由于您可能想要重写其他Qt方法,因此该类Qt必须是类标头中的最后一个。通过不__init__使用合作超级(即使可以),Qt可以避免当其他方法被覆盖时进一步出现错误。