蟒蛇。如何从字典中调用函数?

时间:2019-01-20 12:47:09

标签: python dictionary

我想拥有一个字典,其中某个键调用一个值...该值是一个函数,我希望该函数执行。 下面是我尝试执行的操作,但是我得到的只是它在内存中存储的值。

class Testing:
    def __init__(self):
        self.method = {'Method1': self.another, 'Method2': self.there}

    def launch(self, input):
        print(self.method[input])

    @staticmethod
    def another():
        print('This print statement should pop out.')

    @staticmethod
    def there():
        print('This should not appear.')

new = Testing()
new.launch('Method1')

我从中得到的结果是:

<function Testing.another at 0x01519540>

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

您缺少实际的函数调用:(请注意最后添加的()

def launch(self, input):
        print(self.method[input]())
相关问题