473,396 Members | 1,779 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,396 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 2107
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.