« ICTスクール日記 |Main| ┣¨┣¨┣¨┣¨┣¨┣¨┣¨┣¨日記 »

« Re: 先頭から2文字ずつ取る | Python | 「Jythonでインタラクティブ・ハッキング」@Python Developers Camp 2007 Winter »

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()の返す辞書が不自然な挙動をしているような。

2.1 Built-in Functions

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の説明にちゃんと書いてあったけどもなぁ。

トラックバック(Trackback)

Trackback URL: http://www.nishiohirokazu.org/mt/mt-tb.cgi/466

ご意見・ご感想をお送りください(フィードバック)

(フィードバックはメールで送信され、基本的に表示されませんが、内容によっては公開させていただくこともございます。ご了承ください。Your comment doesn't appear the page immediately. If the comment has value to other people, it will be put on the page or subsequent entries. Thank you.)

上の情報は、いずれも未記入でかまいません。 All of above questions are optional.