静的スコープ
昨日の続きです。ちょっとわからなくなってきました。
>>> msg="A" >>> def f1(): print msg >>> f1() A >>> msg="A" >>> def f1(): msg="B" print msg >>> f1() B
ここまではいいんですが、
>>> msg="A" >>> def f1(): print msg msg="B" >>> f1() Traceback (most recent call last): File "<pyshell#64>", line 1, in -toplevel- f1() File "<pyshell#63>", line 2, in f1 print msg UnboundLocalError: local variable 'msg' referenced before assignment
うーん。これは、、、f1()内のmsgがグローバルなのかローカルなのかは、実行時ではなくコンパイル時に決まるということでしょうか?