Python permanent assignment variables
I have just started python and came across something kind of strange.
The following code assigns a co-ordinate of x=1 and y=2 to the variable test. The test2 variable assigns itself the same value as test and then the [x] value for test2 is changed to the old [x] value minus 1. This works fine, however, when the last part is executed, not only does it minus 1 from the [x] value in test2, it does the same to the [x] value in the test variable too.
test = [1,2];
test2 = test;
test2[1] = test2[1] - 1;
I found doing the following worked fine but I still don't understand why the first method开发者_如何学Go changes the test value as well as the test2 value.
test = [1,2];
test2 = test;
test2 = [test2[0] -1 ,test2[1]];
Could someone please explain why this happens.
Thank You TheLorax
In Python, like in Java, assignment per se never makes a copy -- rahter, assignment adds another reference to the same object as the right-hand side of the =
. (Argument passing works the same way). Strange that you've never heard of the concept, when Python is quite popular and Java even much more so (and many other languages tend to work similarly, possibly with more complications or distinguos).
When you want a copy, you ask for a copy (as part of the expression that is on the right-hand side, or gets used to get the argument you're passing)! In Python, depending on your exact purposes, there are several ways -- the copy
module of the standard library being a popular ones (with functions copy
, for "shallow" copies, and deepcopy
-- for "deep" copies, of course, i.e., ones where, not just the container, but every contained item, also gets deep copied recursively).
Often, though, what you want is "a new list" -- whether the original sequence is a list, a copy, or something else that's iterable, you may not care: you want a new list
instance, whose items are the same as those of "something else". The perfect way to express this is to use list(somethingelse)
-- call the list
type, which always makes a new instance of list, and give it as an argument the iterable whose items you want in the new list. Similarly, dict(somemapping)
makes a new dict
, and set(someiterable)
makes a new set -- calling a type to make a new instance of that type is a very general and useful concept!
It's because when you do test2 = test
you are not copying the contents of the list, but simply assigning test2
a reference to the original list. Thus, any changes to the test2
will also affect test
.
The right way to do this is using deepcopy()
from the copy module (or copy()
if you can get away with a shallow copy.
import copy
test2 = copy.deepcopy(test) # deep copy
test2 = copy.copy(test)) # shallow copy
test2 = test[:] # shallow copy using slices
See this page for a more in-depth explanation as well as other methods to copy a list.
[1,2]
is an array and when you assign it to test
you assign a reference to that array to test. So when you do test2=test
, you are assigning the reference to yet another variable. Make changes to any variable and the array will get changed.
Your second example works, because you create a new array altogether. You might as well have done it like this:
test = [1,2]
test2 = [test[0]-1, test[1]]
Python does not do assignment.
It does name binding.
There is a difference, and it is explained wonderfully and in detail here.
In your first example: Your first line binds the name "test" to a list object created by the "[1,2]" expression. Your second line binds the name "test2" to the same object that is bounded to "test". Your third line mutates the object bound to "test2", which is the same as the object bound to by "test".
In your second example: Your first line binds the name "test" to a list object created by the "[1,2]" expression. Your second line binds the name "test2" to the same object that is bounded to "test". Your third line binds the name "test2" to a new object, created by the expression "[test2[0]-1, test21]".
If you really want two independent lists with the same values bound to different variables, you need to:
>>> test = [1, 2]
>>> test2 = list(test)
>>> test2[0] = 3
>>> print(test)
[1, 2]
>>> print(test2)
[3, 2]
Note that this is a shallow-copy list, so changing the state of the object of an element in test2
will, of course, effect test
if the the same object is found in that list.
p.s. Python. Semi-colons. Ugly. Unnecessary. Do not use.
精彩评论