4

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?

1
  • 2
    Change lis = lis[3:] + lis[:3] to lis[:] = lis[3:] + lis[:3] in function split Commented Nov 5, 2013 at 4:48

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

The assignment to lis is a local variable and is not updating the list. That is easy to see in the python tutor visualization of your program.

Also, see this blog post for a clear explanation of what is going on. Here's a short recap:

  • Names refer to values.
  • Many names can refer to one value.
  • Names are reassigned independently of other names.
  • Assignment never copies data.
  • Changes in a value are visible through all of its names.
  • Python assigns mutable and immutable values differently.
  • References can be more than just names.

In your code, use a global declaration to see how it works different than the default local assignment:

def split(lis):
    global lis

    lis = lis[3:] + lis[:3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.