0
a = b = ['a', 'b'] 
b = ['x', 'y'] # reassigns the name b
print a # still the original list

a = b = ['a', 'b']
b[:] = ['x', 'y'] # changes the existing list bound to a 
print a # a changed too 

How come a is linked to b? What kind of mechanism does python use to handle chained assignments of mutable data types?

Maaverik
  • 193
  • 2
  • 10
  • *"What kind of mechanism does python use to handle chained assignments of mutable data types?"* - no particular mechanism, it just assigns the same object to multiple names; it's the same as doing `a = [...]` then `b = a`. – jonrsharpe Mar 17 '16 at 09:40
  • 1
    *How come a is linked to b?* possibly because you said `a = b` – Tim Mar 17 '16 at 09:40

0 Answers0