How to call static method by reference in python
本问题已经有最佳答案,请猛点这里访问。
请看下面的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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() |
如果我现在
1 2 3 4 5 6 7 | <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 |
为了从类内部调用静态方法,需要将其解包。把