This is a Python function parameter passing question. I want a Python function to adjust the size a numpy array which is one of the functions reference parameters.
The content of the passed array appears to change, inside and outside the function. Somehow the updated size/shape of the array object is not being exported from the function, even though I thought Python passed parameters by reference. I am new to Python programming and would have expected all aspects of the object to be updated by reference. Do I need to explicitly "export" the change?
#!/opt/local/bin/python2.7
# Function Test returning changed array
import numpy
def adjust( a1, a2 ) :
" Adjust passed arrays (my final function will choose which one to adjust from content) "
print str(a1.shape) + " At start inside function"
a1[-1,0] = 99
a1 = numpy.delete(a1, -1, 0)
print str(a1.shape) + " After delete inside function"
return None
d1 = numpy.array( [ [ 1, 2, 3],
[11, 12, 13],
[21, 22, 23],
[31, 32, 33] ] )
d2 = numpy.array( [ [ 9, 8, 7],
[19, 18, 17] ] )
print str(d1.shape) + " At start"
# Let us delete the last row
d1 = numpy.delete(d1, -1, 0)
print str(d1.shape) + " After delete"
# Worked as expected
# So far so good, now do it by object reference parameters in a function......
adjust( d1, d2 )
print d1
print str(d1.shape) + " After function delete return"
# Reference fails to update object properties
Somehow the referenced array object is not getting it's size/shape attributes updated. There should only be 2 rows in the returned array.
(4, 3) At start
(3, 3) After delete
(3, 3) At start inside function
(2, 3) After delete inside function
[[ 1 2 3]
[11 12 13]
[99 22 23]]
(3, 3) After function delete return
So the mainline/global code works as expected, the function fails to adjust the size, but the now deleted line at the end shows the updated data. Remembering the final function will select which one of several parameters to adjust, how do I fully export the changed shape/size of the parameter from the function?