【发布时间】:2017-04-10 13:00:19
【问题描述】:
一件事我看不懂,请解释一下
如果我们有这个代码
import sys
from test import bye, hello
class Example:
COMMANDS = {
'h': hello(), # print 'hello'
'b': bye(), # print 'bye'
}
def __init__(self, arg):
self.arg = self.check(arg)
def check(self, arg):
return self.COMMANDS.get(arg)
if __name__ == '__main__':
Example(sys.argv[1])
我运行脚本python test2.py h
如果类返回hello,这将是合乎逻辑的,因为参数是h
但是类返回
hello
bye
如何通过字典键正确调用函数?谢谢。
【问题讨论】:
标签: python python-3.x class dictionary