So I am working with lists in python 3.3, and here is my example code:
def change_to_z(lis):
lis[3] = 'z'
def change_to_k(lis):
lis[4] = 'k'
def split(lis):
lis = lis[3:] + lis[:3]
totest = ['a', 'b', 'c', 'd', 'e', 'f']
change_to_z(totest)
print(totest)
change_to_k(totest)
print(totest)
split(totest)
print(totest)
and the output:
['a', 'b', 'c', 'z', 'e', 'f']
['a', 'b', 'c', 'z', 'k', 'f']
['a', 'b', 'c', 'z', 'k', 'f']
Notice how when I called the first two functions, I was able modify the list, while totest always referred to the list, even as it was changed.
However, with the third function, the variable totest no longer refers to the latest modified version of the list. My debugger tells me that within the function "split", the list is flipped, but outside of the function, it is not flipped. Why is it that the variable name no longer refers to the list?
Why does this happen? And with what operators does this happen? Why is it sometimes that the variable name still refers to the list ever after it is modified in a function, but it doesn't behave that way with other operators?
lis = lis[3:] + lis[:3]tolis[:] = lis[3:] + lis[:3]in functionsplit