473,796 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2125
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...@gm ail.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
10543
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: http://beluga.eos.ubc.ca/~tang/softwares/python/ Here is the doc string: # Before using whos, do:
2
2796
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 this and what should I do ? Is this bug in numpy or in Numeric? >>m #numpy array array(, , , ..., ,
5
4867
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 types. First there is some inefficiency in calculations. And then you get data inflation and questionable dependencies - e.g. with pickle,ZODB,mpi's ... : 0.0...
2
2040
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 roots are 0 and -1
6
1775
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
1426
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
225
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 = foo.append(2)
2
1701
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 array with a single element:
3
6842
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 astounding for me that the numpy Version is almost 7 times slower than the pure python version. I tryed to find out if i am doing something wrong but wasn't able to find any answer.
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.