0

The question is pretty simple, suppose you have the following code:

a = (5,6,[7,8])
a[2] += [8] 

It raises an exception as would be predicted, because tuples are immutable, but print(a) results in (5,6,[7,8,9]) I wanted to figure out what is going on behind the scenes.

Alternatively if you use extend on a[2] the exception is not thrown.

Vahagn Tumanyan
  • 500
  • 1
  • 13
  • 29
  • @hygull this is not true. Indeed, `b += something` works in-place on `b` if `b` and `something` are list objects. It is idiomatic for containers to use in-place operations for augmented assignment operators, and their non augmented counter parts should return new objects, but the language doesn't enforce this, and you can implement those operators to do whatever you like. – juanpa.arrivillaga Jul 24 '18 at 15:45
  • Yes, you're correct. Thanks for the knowledge. – hygull Jul 24 '18 at 16:06

0 Answers0