値を関数にしたいが予期せぬ動作が発生したOrderedDictがある。初期化:Python dict/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ループまで待つことを望みます。
何が起こっているのですか?
'call_func_A'は関数で、' call_func_A(arg1、arg2) 'は関数を呼び出しています。 –