I am not very sure how to explain this question, but lets look at the code below:
import numpy as np
def glb_mod(s_np,s):
s_np.shape = (2,-1)
s=2
return s
if __name__=='__main__':
a=1
a_np = np.arange(10)
print ('a, a_np initialized as')
print(a , a_np)
glb_mod(a_np,a)
print ('a, a_np are now')
print (a, a_np)
I have two global variables named:
a, a_np
after run through the function
glb_mod()
results:
a, a_np initialized as
1 [0 1 2 3 4 5 6 7 8 9]
a, a_np are now
1 [[0 1 2 3 4]
[5 6 7 8 9]]
why "a_np" changed but "a" not change? Just wonder how should I modify the code so that when passing global variable "a_np" into function, "a_np" will not change after run throw the function "glb_mod()"?