2012-04-20 13 views
7

以下のコードは、同じことを3回印刷することになっています。それはなぜですか?QObject派生物のバリアブルアクセス奇妙さ

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を上書きすることはありませんAインスタンスを "cls.instance"と戻しますか?

アサインする前にアトリビュートにアクセスしていないと(コメント付きのコード行を参照)、アサインしても問題ありません。

(PySide 1.1.0で)次のように私はこれを再現することができ:

  • Windows 7の64ビットのPython 2.7.1 32ビット:
  • Windows 7の64ビットのPython 2.7動作0.3 32ビット:
  • Windows 7の64ビット版で動作します、Pythonの3.2.3は、32ビット:
  • のUbuntu 11.10 64ビット、Pythonの2.7.2+に失敗した64ビットのUbuntu 11.10
  • の作品、 Python 3.2.2:失敗する

更新:私はUbuntuでPython 3.2.2でPySide 1.1.1をコンパイルすることができましたが、問題は解決しません。

+0

'Pythonの2.7.3とWindows上でPySide 1.1.0を。 – reclosedev

+0

Python 2.7.3をUbuntu 12.04にインストールした場合、PySideとPyQt4の両方で期待される結果が得られます。 – pwuertz

+0

Python 3、誰ですか?それはここの問題の要点だと思われます。 –

答えて

1

これはPython 3.2.3/PySide 1.1.0、Ubuntu 12.04で確認できます。 PyQt4と同じインストールで動作します。

これはPySideのバグです。まだそうしていない場合は、バグレポートを提出する必要があります。

私が唯一の例を少し、例もセグメンテーション違反変更する場合:私は期待通りの結果を得ている32bit`

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() 
関連する問題