Obviously, just like what the 2nd floor said.
Code 1 l2 = l1, l2 += [4], all operations are l1, similar to pointers, referencing Shenma, if you don’t understand it, just think about it.
Code 2 l2 = l1, l2 = l2 + [4], this is obviously a reassignment of l2. You can write l3 = l2 + [4], l2 += [4]. Then you will know the result.
You can learn about python’s deep copy and shallow copy
There is a very good answer in stackoverflow on the second floor. Just use id() to check the memory address
>>> l = []
>>> id(l)
13043192
>>> l += [3]
>>> id(l)
13043192
>>> l = l + [3]
>>> id(l)
13059216
Don’t worry too much, just look at the memory address and it will be clear at a glance.
id is a built-in function of python, what's id? ...
《'id' is a bad variable name in Python》
id() is a fundamental built-in:
Help on built-in function id in module builtin:
id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory
address.)
What I can think of is that some classes implement __iadd__ and behave differently from __add__. List's += is an example. Ordinary + will return a new list instance, while += is Directly manipulate the list itself.
If you can confirm that i and x are two numbers, saying that i += x will cause problems is a bit nitpicking and dogmatism.
The poster here has some misunderstandings, i += x, this is a kind of syntax sugar, what does it really look like after expansion?
In C language, the true form of i += x is i = i+x. Here i is reassigned, so the pointer address of i will change.
But this is not the case in Python. The true form of i += x is i.extend(x), where the pointer address of i has not changed, only the value of i has been changed.
refer: http://stackoverflow.com/questions/2347265/why-does-behave-unexpectedly-on-lists
thank to: @WKPlus
What Floor 4 said is so right. There is a chance that the MD5 values of two different files are the same. The question is, have you ever encountered them?
Look at the comparison of the two pieces of code below:
Code 1:
Code 2:
See here for a detailed explanation: http://stackoverflow.com/questions/2347265/why-does-behave-unexpectedly-on-lists
Obviously, just like what the 2nd floor said.
Code 1 l2 = l1, l2 += [4], all operations are l1, similar to pointers, referencing Shenma, if you don’t understand it, just think about it.
Code 2 l2 = l1, l2 = l2 + [4], this is obviously a reassignment of l2. You can write l3 = l2 + [4], l2 += [4]. Then you will know the result.
You can learn about python’s deep copy and shallow copy
There is a very good answer in stackoverflow on the second floor. Just use id() to check the memory address
Don’t worry too much, just look at the memory address and it will be clear at a glance.
id is a built-in function of python, what's id? ...
《'id' is a bad variable name in Python》
id() is a fundamental built-in:
Help on built-in function id in module builtin:
What I can think of is that some classes implement
__iadd__
and behave differently from__add__
. List's+=
is an example. Ordinary+
will return a new list instance, while+=
is Directly manipulate the list itself.If you can confirm that
i
andx
are two numbers, saying thati += x
will cause problems is a bit nitpicking and dogmatism.Mainly look at i, if i is an expression
In i+=x, i is only calculated once
i = i+x i needs to be calculated 2 times
Then, if there are modification variables in the i expression, the values of the two i in i = i+x may be different
This example should be common in various grammar books
The above is for reference types, there should be no difference for value types.
The core issue is to prevent side effects. Make your code behave consistently.
The poster here has some misunderstandings, i += x, this is a kind of syntax sugar, what does it really look like after expansion?
In C language, the true form of i += x is i = i+x. Here i is reassigned, so the pointer address of i will change.
But this is not the case in Python. The true form of i += x is i.extend(x), where the pointer address of i has not changed, only the value of i has been changed.
refer: http://stackoverflow.com/questions/2347265/why-does-behave-unexpectedly-on-lists
thank to: @WKPlus
What Floor 4 said is so right. There is a chance that the MD5 values of two different files are the same. The question is, have you ever encountered them?