In multiple-target assignment there is one object which is shared by all three variables.
This behavior is fine for immutable types.
a = b = 0
b = b + 1
print( a, b )
In case of mutable object such as a list or dictionary:
a = b = []
b.append(42)
print( a, b )
Here, because a and b reference the same object, appending to it in place through b will impact a as well.
To avoid the issue, initialize mutable objects in separate statements instead:
a = [] b = [] # a and b do not share the same object b.append(42) # from ww w. j a v a 2 s .co m print( a, b )
A tuple assignment like the following has the same effect-by running two list expressions, it creates two distinct objects:
a, b = [], [] # a and b do not share the same object