【问题标题】:Variable access weirdness in a QObject derivativeQObject 导数中的变量访问怪异
【发布时间】:2012-04-20 13:37:30
【问题描述】:

下面的代码应该打印同样的东西 3 次。为什么没有?

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()

预期结果:

<__main__.A object at 0x1310c20>
<__main__.A object at 0x1310c20>
<__main__.A object at 0x1310c20>

实际结果:

<__main__.A object at 0x2242878>
1
1

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,但并没有解决问题。

【问题讨论】:

  • Python 2.7.3 and PySide 1.1.0 on Windows 32bit 我得到了预期的结果。
  • Ubuntu 12.04 上的 Python 2.7.3,PySide 和 PyQt4 都提供了预期的结果。
  • Python 3,有人吗?这似乎是问题的症结所在。

标签: python qt4 python-3.x pyside


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多