bartolome.sin...@gmail.com writes: > Hi, > > I thought that x += ... was the same than x = x + ..., but today I > have realized it is not true when operating with mutable objects. > > In Python 3.3 or 2.7 IDLE (Windows) compare: > >>> a = [3] > >>> b = a > >>> a = a + [1] > >>> b > [3] > > and > >>> a = [3] > >>> b = a > >>> a += [1] > >>> b > [3, 1] > > Is this behaviour explained in the Python documentation?
Yes, it's documented in the language reference, specifically in the latter half of the paragraph quoted below. <http://docs.python.org/3/reference/simple_stmts.html#assignment-statements> # An augmented assignment expression like x += 1 can be rewritten as x # = x + 1 to achieve a similar, but not exactly equal effect. In the # augmented version, x is only evaluated once. Also, when possible, # the actual operation is performed in-place, meaning that rather than # creating a new object and assigning that to the target, the old # object is modified instead. -- http://mail.python.org/mailman/listinfo/python-list