Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 odd function behavior on list

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?

like image 870
Sihan Zheng Avatar asked Oct 22 '25 22:10

Sihan Zheng


1 Answers

You are wrong about scope of the function.

If you really want to change the supplied list, you can try out of one the following 2 methods.

def split(lis):
    global lis
    lis = lis[3:] + lis[:3]

Or better

def split(lis):
    lis[:] = lis[3:] + lis[:3] # Credits go to drewk for pointing this out

When you are doing

def split(lis):
    lis = lis[3:] + lis[:3]

In the split function, you are creating a local list with the same identifier as that of the supplied list. But, these two won't clash since passed list is global.

If you want to play around & know if they are really different, use id()

lis = [1, 2, 3]
def split(lis):
    lis = lis[3:] + lis[:3]
    print id(lis)
split(lis)
print id(lis)

Output of the above code

40785920
40785600

Notice how the memory locations are different

like image 86
shad0w_wa1k3r Avatar answered Oct 24 '25 12:10

shad0w_wa1k3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!