这是我的代码:
def test1():
print("nr1")
def test2():
print("nr2")
def test3():
print("nr3")
def main():
dictionary = { 1: test1(), 2: test2(), 3: test3() }
dictionary[2]
if __name__ == "__main__":
main()
此代码返回:
^{pr2}$
我需要在代码中做什么更改才能得到:
nr2
我使用的是python2.7.13。在
Tags:
下面的行实际调用每个函数并将结果存储在字典中:
这就是为什么会看到三行输出。正在调用每个函数。因为函数没有返回值,所以值
^{pr2}$None
存储在字典中。打印(print(dictionary
):相反,将函数本身存储在字典中:
print(dictionary)
的结果:然后使用字典查找来获取函数,然后调用它:
创建字典时省略函数调用,只调用
dictionary[2]
返回的内容:不要调用dict内部的函数;调用dict查找的结果。在
相关问题 更多 >
编程相关推荐