2017-03-24 11 views
2

次のコードスニペットが機能しない理由を説明できますか?Pythonで静的メソッドを渡す

class A: 
    @staticmethod 
    def f(): 
     print('A.f') 

    dict = {'f': f} 

def callMe(g): 
    g() 

callMe(A.dict['f']) 

それは

class A: 
    @staticmethod 
    def f(): 
     print('A.f') 

    dict = {'f': f} 

def callMe(g): 
    g() 

callMe(A.f) 

または

class A: 
    @staticmethod 
    def f(): 
     print('A.f') 

    dict = {'f': lambda: A.f()} 

def callMe(g): 
    g() 

callMe(A.dict['f']) 

にそれを変更することが期待される結果

を与え、Interesingly

TypeError: 'staticmethod' object is not callable 

を生み出します

A.f 

私の知る限り動作は、Python 2で同じと3. A内部fオブジェクトはdescriptor、しない静的メソッド自体は

答えて

3

で見るように - それ戻り staticmethod呼び出されたときAのインスタンスと;リンクを読み、 "ディスクリプタプロトコル"を参照して、これがどのように機能するかについての詳細を調べてください。メソッド自体は、ディスクリプタの__func__属性として格納されます。

あなた自身のためにこれを見ることができます:

>>> A.f 
<function A.f at 0x7fa8acc7ca60> 
>>> A.__dict__['f'] 
<staticmethod object at 0x7fa8acc990b8> 
>>> A.__dict__['f'].__func__ # The stored method 
<function A.f at 0x7fa8acc7ca60> 
>>> A.__dict__['f'].__get__(A) # This is (kinda) what happens when you run A.f 
<function A.f at 0x7fa8acc7ca60> 

はまた、あなたが f記述子オブジェクトにアクセスするために A.__dict__を使用できることに注意してください、あなたはそれを格納するための独自の辞書を作成する必要はありません。

1

staticmethodオブジェクトはdescriptorであり、記述子メカニズムを有効にするには、クラスの属性としてアクセスする必要があります。 staticメソッドオブジェクト自体は呼び出し可能ではありませんが、__get__の結果は呼び出し可能です。 this Python bug discussionも参照してください。

関連する問題