S'il vous plaît voir l'exemple suivant:Comment appeler la méthode statique par référence en python
class MyClass(object):
@staticmethod
def __myStaticMethod(someArgs):
pass
MY_SPECIAL_METHOD_LIST = [
__myStaticMethod
]
@staticmethod
def someOtherMethod():
m = MyClass.MY_SPECIAL_METHOD_LIST[0]
print(m)
m()
Si j'exécute maintenant la déclaration MyClass.someOtherMethod()
je reçois une exception:
<staticmethod object at 0x7fd672e69898>
Traceback (most recent call last):
File "./test3.py", line 25, in <module>
MyClass.someOtherMethod()
File "./test3.py", line 21, in someOtherMethod
m()
TypeError: 'staticmethod' object is not callable
Il est évident que m
contient une référence à la méthode statique. Mais je ne peux pas appeler cette méthode. Pourquoi? Que dois-je changer pour appeler cette méthode?
Regardez ici: http://stackoverflow.com/questions/12718187/calling-class-staticmethod-within-the-class-body – haffla
@haffla: très bonne trouvaille. Je vous remercie. –
Intéressant. J'ai cherché une réponse mais je ne l'ai pas trouvée. Merci. –