Augmented Assignment
Comments
- Intent is in-place modification of mutable objects
>>> a = [1,2]
>>> id(a)
831988
>>> a += [3]
>>> id(a)
831988 # Note: a is same object as before
>>> a = a + [4]
>>> id(a)
831628 # Note: a is now a different object
>>>
- For immutable objects, no in-place modification occurs
>>> s = "Hello"
>>> t = s
>>> s += "World" # Creates a new string "HelloWorld"
>>> print t
Hello
>>>
|