In Python, if I run
test_list = []
test_list += [1]
print(id(test_list))
I can find the identity of test_list
has not changed.
However, if I run
test_list = []
test_list = test_list + [1]
print(id(test_list))
the output shows me the identity has changed.
What's the difference?
I found this while coding a recursive function with a list as an argument where the variable outside the function is affected because of the operator +=
.
Isn't a += b
identical to a = a + b
?