【发布时间】:2017-09-05 11:56:22
【问题描述】:
我有一个 OrderedDict,我想将其值作为函数,但遇到了意外行为。初始化:
from collections import OrderedDict
options_dict=OrderedDict(["A",call_func_A(arg1,arg2)],
["B",call_func_B(arg1,arg3)],
["C",call_func_C(arg1,arg4)]
# Select options
options=["A","C"]
# Execute
result={}
for opt in options:
result[opt]=options_dict[opt]
# Return result (or whatever)
print result
函数 call_func_A、call_func_B 和 call_func_C 在声明 options_dict 时被执行,而不是在随后的 for 循环中执行。
我希望函数调用等到 for 循环。
发生了什么事?
【问题讨论】:
-
call_func_A是函数,call_func_A(arg1,arg2)是调用函数
标签: python dictionary ordereddictionary