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
     >>>
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 8
July 26, 2001, beazley@cs.uchicago.edu
>>>