[Numpy-discussion] bug or feature?

humufr at yahoo.fr humufr at yahoo.fr
Thu Feb 8 16:21:09 EST 2007


I have a big problem with numpy, numarray and Numeric (all version)

If I'm using the script at the bottom, I obtain these results:

var1 before function is  [3 4 5]
var2 before function is  1
var1 after function must be [3 4 5] is  [ 9 12 15]     <------ problem
var2 after function must be 1 is  1
var3 must be the [9 12 15] is  [ 9 12 15]
var4 must be the 'toto' is  toto


I'm very surprised by the line noted. I always thinking that the input 
variable didn't change the variable itself outside the function. It's the 
comportement for var2 but var1 is changed and it's a big problem (at least 
for me). The only object in python with this behavior are the numeric object 
(Numeric, numarray or numpy), with list or other kind of object I have the 
expected result (the var1 before to go inside the function)

I can't keep the input variable if I'm not doing a copy before to call a 
function...

Is it normal and so do I have to do a copy of the input data each time I'm 
calling a function? 

Thanks


Nicolas

#!/usr/bin/env python
import numpy
print "numpy version ", numpy.__version__
def test(var1,var2):
        #print "var1 input function is",var1
        #print "var2 input function is",var2
        var1 *=3
        var2 = 'toto'
        return var1,var2

var1=numpy.array([3,4,5])
var2=1
print "var1 before function is ",var1
print "var2 before function is ",var2
var3,var4=test(var1,var2)
print "var1 after function must be [3 4 5] is ",var1
print "var2 after function must be 1 is ", var2
print "var3 must be the [9 12 15] is ", var3
print "var4 must be the 'toto' is ", var4



More information about the NumPy-Discussion mailing list