[IPython-dev] dictionaries and %run oddity

Kevin Buchs kevin.buchs at gmail.com
Wed Nov 30 08:09:55 EST 2011


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:

1. Create a file, x.py with these contents

def a():
    global b
    b={}
    b[1]=(2,3)


2. In a iPython shell execute these commands:

%run x
a()
b


which results in b being undefined at the shell level.

3. Edit x.py to contain this line at the end:

a()


4. Now, in the shell, execute these commands:

run x
b


which results in the correct value of b at the shell level.

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.

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 *only seen this with
a dictionary.*

- Kevin Buchs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20111130/e370e052/attachment.html>


More information about the IPython-dev mailing list