I found a strange behavior with regard to global dictionaries and the %run command. Perhaps someone can explain this to me. Here is the scenario:<div><br></div><div>1. Create a file, x.py with these contents</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;">
<div><div>def a():</div><div>    global b</div><div>    b={}</div><div>    b[1]=(2,3)</div></div></blockquote><div><br></div>2. In a iPython shell execute these commands:<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;">
<div>%run x</div><div>a()</div><div>b</div></blockquote><br><div>which results in b being undefined at the shell level.</div><div><br></div><div>3. Edit x.py to contain this line at the end:</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;">
<div>a()</div></blockquote><br><div>4. Now, in the shell, execute these commands:</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>run x</div><div>b</div></blockquote><br><div>which results in the correct value of b at the shell level.</div>
<div><br></div><div>My understanding is the iPython runs %run commands in a new instance of python, but then the namespace of that instance is inherited by the iPython level namespace. With that, it would seem to matter little that the definition of a() is separated from the invocation. </div>
<div><br></div><div>The unusual thing is that I found this behavior is not present if the global variable is an integer or list. So far, I have <b>only seen this with a dictionary.</b></div><div><br></div><div>- Kevin Buchs</div>
<div><br></div>