This is python 2.7.10.
I am giving one example of what I am asking. I am swapping the first and second element of an array via a function.
#!/usr/bin/python
def change(array):
array2 = array
array2[0], array2[1] = array2[1], array2[0]
return array2
a = [100, 200, 300]
print "Original array: "
print a #[100, 200, 300]
print "Feeding to some function"
b = change(a)
print "Original array becomes: "
print a #[200, 100, 300] <- Unexpected
print "Result of my function: "
print b #[200, 100, 300]
I thought the function should not alter the value/status of feed-in parameters. In other words, array a
should be still [100, 200, 300]
after running through function change
.
My question is following:
- Am I doing wrong? I know how to use
__main__
related. - Could you guys walk me through "wired" behavior? Is there any term/page to describe such a behavior of python language?
To me, python works like:
Input -> function -> Output + Input_altered
But I assumed it should work like:
Input -> function -> Output + Input