Function to resize global numpy array interactively in ipython

Discussion in 'Python' started by David Sanders, Oct 29, 2007.

  1. Hi,

    I have a script with function definitions which I load into ipython
    for interactive use.
    These functions modify a global numpy array, whose size I need to be
    able to change interactively. I thus have a script which looks like
    this:

    from numpy import *

    def do_resize(N):
    global a
    a = resize(a, N)

    a = array([])

    N=10; do_resize(N)
    print "Length of a is: ", len(a)
    N=20; do_resize(N)
    print "Length of a is: ", len(a)


    If I run this in ipython, using "run resize.py", it correctly outputs
    10 and then 20.
    If I now type *interactively* N=30; do_resize(N), then the length of
    a is still 20, rather than 30 as I was hoping -- somehow I seem to be
    now dealing with a different copy of a?

    Doing the same thing in idle works as I expect, i.e. interactively the
    size is changed to 30.

    Could somebody please explain what's going on, and how I solve the
    problem?

    Thanks and best wishes,
    David.
     
    David Sanders, Oct 29, 2007
    #1
    1. Advertising

  2. David Sanders

    Robert Kern Guest

    David Sanders wrote:
    > Hi,
    >
    > I have a script with function definitions which I load into ipython
    > for interactive use.
    > These functions modify a global numpy array, whose size I need to be
    > able to change interactively. I thus have a script which looks like
    > this:
    >
    > from numpy import *
    >
    > def do_resize(N):
    > global a
    > a = resize(a, N)
    >
    > a = array([])
    >
    > N=10; do_resize(N)
    > print "Length of a is: ", len(a)
    > N=20; do_resize(N)
    > print "Length of a is: ", len(a)
    >
    >
    > If I run this in ipython, using "run resize.py", it correctly outputs
    > 10 and then 20.
    > If I now type *interactively* N=30; do_resize(N), then the length of
    > a is still 20, rather than 30 as I was hoping -- somehow I seem to be
    > now dealing with a different copy of a?
    >
    > Doing the same thing in idle works as I expect, i.e. interactively the
    > size is changed to 30.
    >
    > Could somebody please explain what's going on, and how I solve the
    > problem?


    By default, %run executes the script in its own namespace. Then the interactive
    prompt's namespace gets updated with the values in that namespace. The global
    statement refers to that initial namespace, not the one of the interactive
    prompt. Give "%run -i resize.py" a try, though. That should execute the code in
    the interactive prompt's namespace.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Oct 29, 2007
    #2
    1. Advertising

  3. On Oct 29, 11:07 am, Robert Kern <> wrote:
    > David Sanders wrote:
    > > Hi,

    >
    > > I have a script with function definitions which I load into ipython
    > > for interactive use.
    > > These functions modify a global numpy array, whose size I need to be
    > > able to change interactively. I thus have a script which looks like
    > > this:

    >
    > > from numpy import *

    >
    > > def do_resize(N):
    > > global a
    > > a = resize(a, N)

    >
    > > a = array([])

    >
    > > N=10; do_resize(N)
    > > print "Length of a is: ", len(a)
    > > N=20; do_resize(N)
    > > print "Length of a is: ", len(a)

    >
    > > If I run this in ipython, using "run resize.py", it correctly outputs
    > > 10 and then 20.
    > > If I now type *interactively* N=30; do_resize(N), then the length of
    > > a is still 20, rather than 30 as I was hoping -- somehow I seem to be
    > > now dealing with a different copy of a?

    >
    > > Doing the same thing in idle works as I expect, i.e. interactively the
    > > size is changed to 30.

    >
    > > Could somebody please explain what's going on, and how I solve the
    > > problem?

    >
    > By default, %run executes the script in its own namespace. Then the interactive
    > prompt's namespace gets updated with the values in that namespace. The global
    > statement refers to that initial namespace, not the one of the interactive
    > prompt. Give "%run -i resize.py" a try, though. That should execute the code in
    > the interactive prompt's namespace.


    Great, that's exactly what I needed!
    Thanks very much.

    David.
     
    David Sanders, Oct 29, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drife
    Replies:
    1
    Views:
    365
    Travis E. Oliphant
    Mar 1, 2006
  2. Replies:
    2
    Views:
    491
    Robert Kern
    Nov 13, 2007
  3. terminus
    Replies:
    0
    Views:
    418
    terminus
    Feb 26, 2008
  4. bdb112
    Replies:
    2
    Views:
    1,175
    Jason Scheirer
    Mar 12, 2009
  5. Wolfram Brenig

    ipython -wthread vs. ipython -pylab

    Wolfram Brenig, Sep 12, 2011, in forum: Python
    Replies:
    0
    Views:
    347
    Wolfram Brenig
    Sep 12, 2011
Loading...

Share This Page