如何在python中通过引用调用静态方法

时间:2017-02-16 12:50:43

标签: python static-methods invoke

请参阅以下示例:

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包含对静态方法的引用。但我不能称这种方法。为什么?为了调用此方法,我需要更改什么?

1 个答案:

答案 0 :(得分:0)

为了从你的类中调用静态方法,你需要打开它。将$messages = $user->messages()->latestSendersMessages(); 更改为m(),您就会很好。