我有一個OrderedDict的值,我想成爲功能,但遇到意外的行爲。初始化:Python的字典/ 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)'正在調用函數 –