+ 1
Difference between a+=b and a=a+b?
I realized that there is a difference how python handles the the above mentioned statements. As far as I figured out += will deal with a reference while the complete sentence will make a new variable. Can someone shade some light on this issue? Is there something I missed in the basic documentation? Attached you can finde a code sample Thanks experts! https://code.sololearn.com/c5DANL2pPhX1/?ref=app
8 ответов
+ 1
You are right, RGermer!
For mutables like lists, x += y is equivalent to x = operator.iadd(x,y). The operator __iadd__ modifies the first argument in-place by adding the second one to it. For comparison, regular __add__ only returns sum of the two numbers without modifying either of them. So x = x + y will create a NEW list by adding x and y, and then assign that list to x. Try this:
x = [1]
print(id(x))
x += [2]
print(id(x))
x = x + [3]
print(id(x))
The first operation shouldn't change the address of x in memory (as checked by id()), as no new object is created. But it's different for the second operation.
For immutables (like numbers, strings, and tuples), I believe, since any modification of x is not possible, both x += y and x = x + y do the exact same thing. (At least, the addresses are changed in both cases. But there may be a bit more going on in the background.)
0
There is no difference. x += y doesn't create a new variable.
0
That‘s what I thought. But the code attached yield different results. So is there something other what I missed?
0
https://code.sololearn.com/ckz0vIeG9INQ/?ref=app
I don't think theirs a difference
0
Thank you - the id() command gives the clue!
By using += the id of the class attribute and the temp variable are identical. So manipulating the temp variable also changes the class attribute ( no need to use setattr() in my example).
What is even more strange is that by using += different instances of the room class shaering the same attribute id.
I do not understand the logic behind this but at least I know how to avoid the problem.
0
RGermer
In your code the three rooms a1, a2, and exit all have different ids. But they are sharing the same list "player". (This is because when you are first defining them, you are not passing any list of players, so it's set to the same default list in the definition of the room class.). That's why a1.player, a2.player, and exit.player have the same id.
Or did I not understand you correctly? Please let me know.
0
I guess you got the root cause for my issue.
During init of my object instances one attribute is filled with an empty list. Python use the same reference for all of these lists(and also for the object attributes).
By changing one of the attributes in a way I did (+=) Python is still using this reference. Therefore this attribute is changed fot all instances of the object - fascinating ;)
Thanks for the solution.
0
RGermer, yes, that's right! If you want to maintain different player lists for different rooms, you'd probably have to supply them new lists during the definition, like
a1=room("a2", [])
a2=room(0, [])
exit=room(0, [])