QObject导数中的变量访问怪异

Ale*_*olm 7 python qt4 pyside python-3.x

下面的代码应该打印三次相同的东西.为什么不呢?

from PySide.QtCore import QObject


class A(QObject):
    instance = 1

    @classmethod
    def test(cls):
        cls.instance  # Remove this line and it prints the right thing
        cls.instance = cls()
        print(cls.__dict__['instance'])
        print(cls.instance)
        print(type.__getattribute__(cls, 'instance'))

A.test()
Run Code Online (Sandbox Code Playgroud)

预期结果:

<__main__.A object at 0x1310c20>
<__main__.A object at 0x1310c20>
<__main__.A object at 0x1310c20>
Run Code Online (Sandbox Code Playgroud)

实际结果:

<__main__.A object at 0x2242878>
1
1
Run Code Online (Sandbox Code Playgroud)

QObject背后的元类甚至不会覆盖getattribute,那么我怎么可能没有使用"cls.instance"获取A实例?

更奇怪的是,在分配属性之前不访问该属性(请参阅注释的代码行)使其工作正常.

我可以重现如下(使用PySide 1.1.0):

  • Windows 7 64位,Python 2.7.1 32位:有效
  • Windows 7 64位,Python 2.7.3 32位:有效
  • Windows 7 64位,Python 3.2.3 32位:失败
  • Ubuntu 11.10 64位,Python 2.7.2+:有效
  • Ubuntu 11.10 64位,Python 3.2.2:失败

更新:我设法在Ubuntu上的Python 3.2.2上编译PySide 1.1.1,并没有解决问题.

mat*_*ata 1

我可以在 Python 3.2.3 / PySide 1.1.0、Ubuntu 12.04 上确认这一点。在同一安装上与 PyQt4 一起使用。

这肯定是 PySide 中的一个错误。如果您还没有这样做,您应该提交错误报告。

如果我只稍微改变一下示例,该示例甚至会出现段错误:

from PySide.QtCore import *

class A(QObject):
    instance = []

    @classmethod
    def test(cls):
        print(cls.instance)
        cls.instance = cls()
        print(cls.__dict__['instance'])
        print("still ok")
        print(cls.instance)
        print("you won't see me")
        print(type.__getattribute__(cls, 'instance'))

A.test()
Run Code Online (Sandbox Code Playgroud)