473,287 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Function to resize global numpy array interactively in ipython

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.

Oct 29 '07 #1
2 2102
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

Oct 29 '07 #2
On Oct 29, 11:07 am, Robert Kern <robert.k...@gmail.comwrote:
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.

Oct 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: benyang22 | last post by:
I have been a long time Matlab user. I Python, I miss Matlab's whos command. So I have written a little utility whos.py, which can be downloaded here:...
2
by: robert | last post by:
in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0 Why is...
5
by: robert | last post by:
Turning algs for old NumPy modules into numpy code I suffer from this: Upon further processing of returns of numpy calculations, lots of data in an apps object tree will become elementary numpy...
2
by: =?ISO-8859-1?Q?Sch=FCle_Daniel?= | last post by:
Hello NG, given this call to roots funtion from pylab In : roots() Out: array() as far as I understand it stands for a0+a1*x+a2*x^2 in the above case it yields 2x^2+2x = 2x(1+x) and the...
6
by: vorticitywolfe | last post by:
Is there a function in Python analogous to the "where" function in IDL? x= print where(x=2) output:
3
by: Anand Patil | last post by:
On May 29, 2008, at 9:38 AM, Gary Herron wrote: If you're using IPython, you can do svd?? .
2
by: Anthony Kuhlman | last post by:
Pythoners, I'm having trouble understanding the behavior of global variables in a code I'm writing. I have a file, test.py, with the following contents foo = def goo(): global foo foo = ...
2
by: Rick Giuly | last post by:
Hello All, Case 1 This generates an error, which makes sense because the argument should be a list of numbers: numpy.array(10,10) Case 2 This does not generate an error and the result is an...
3
by: Rüdiger Werner | last post by:
Hello! Out of curiosity and to learn a little bit about the numpy package i've tryed to implement a vectorised version of the 'Sieve of Zakiya'. While the code itself works fine it is...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.