【发布时间】:2017-07-05 14:17:51
【问题描述】:
请看下面的例子:
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()
如果我现在执行语句 MyClass.someOtherMethod() 我会得到一个异常:
<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
显然m 包含对静态方法的引用。但我不能调用这个方法。为什么?我需要更改什么才能调用此方法?
【问题讨论】:
-
@haffla:很好的发现。谢谢。
-
有趣。我搜索了一个答案,但没有找到那个答案。谢谢。
标签: python static-methods invoke