【发布时间】: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