Pythonのexecfileにlocals()を渡した際の挙動
バージョン2.4.3で。
# tmp.py x = 1 y = 2
def foo():
x = 0
print locals() #=> {'x': 0}
execfile(r"c:\tmp.py", globals(), locals())
print locals() #=> {'y': 2, 'x': 0}
dic = {"x": 0}
print dic #=> {'x': 0}
execfile(r"c:\tmp.py", globals(), dic)
print dic #=> {'y': 2, 'x': 1}
del dic
print locals() #=> {'y': 2, 'x': 0}
locals()["x"] = 1
locals()["z"] = 1
print locals() #=> {'y': 2, 'x': 0, 'z': 1}
foo()
どちらかというとexecfileがというよりlocals()の返す辞書が不自然な挙動をしているような。
locals()
Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter.
localsの説明にちゃんと書いてあったけどもなぁ。