<div>Hi,</div><div> </div><div>So I have a simple test program - test.py:</div><div> </div><div>a = b = 1</div><div>def test1():<br> global a<br> print "a=",a<br> a = 2<br> <br>def test2():<br> global b<br>
print "b =", b<br> b = 2<br> <br>def test3():<br> g = globals()<br> g['b']=4<br> <br> </div><div>test1()<br>print "a=",a</div><div> </div><div>--------------------------------------</div>
<div> </div><div>If I run it in an ipython session, I get the following:</div><div> </div><div>In [2]: run test<br>a= 1<br>a= 2</div><div>In [3]: test2()<br>b = 1</div><div>In [4]: b<br>Out[4]: 1</div><div>In [5]: test3()</div>
<div>In [6]: b<br>Out[6]: 1</div><div>In [7]: a<br>Out[7]: 2</div><div> </div><div>What you can see is that if I run the function (test1) inside the script test.py, it is able to change the global variable. If I run the function (test2) under command prompt, it reads the value of the global variable all right, but is not able to change the value. If I do it differently using globals() as in test3(), I am still not able to change the global variable. </div>
<div> </div><div> </div><div>Now if I run it under ipython's namespace, using run - i, when I run functions under command prompt, I am able to change global variables:</div><div> </div><div>In [9]: run -i test<br>a= 1<br>
a= 2</div><div>In [10]: test2()<br>b = 1</div><div>In [11]: b<br>Out[11]: 2</div><div> </div><div>Why is that?</div><div> </div><div>Thanks,</div><div>Tom</div><div> </div><div> </div>