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):
更新:我设法在Ubuntu上的Python 3.2.2上编译PySide 1.1.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)
| 归档时间: |
|
| 查看次数: |
336 次 |
| 最近记录: |