Skip to content Skip to sidebar Skip to footer

What Was The Motivation For Doing Lists Augmented Assignment (+=) In Place In Python?

>>> list1 = [] >>> list2 = list1 >>> list2 += [1] >>> print list1 [1] Comparing this to >>> list1 = [] >>> list2 = list1 >

Solution 1:

Lists in Python as stored by reference.

This means that when you do list2 = list1, you are not making a copy of the list - you are merely saying "list2 refers to the same thing list1 does," namely, the list you originally created when you did list1 = [].

Python specifies += to mean "append in place" for lists, because most of the time when you're using += on lists, that's what you want to do - you usually don't want to create new lists every single time you add an element.

Thus, when you append to list2, which "refers to the same object list1 does," and then read from list1, you see the appended item, as is expected since both of them point at the same list.

With +, however, a new list is always created because it doesn't make sense to modify either of the operands in place (since a+b doesn't imply the modification of a or b).

Therefore, when you do list2 = list2 + [1], you create a new list that has all of the contents of the original object pointed to by list2 and also 1, and then say that list2 now references that new list. Since it now references a different list than list1 does, when you go to read from list1 you still see the original list without the extra 1.

Solution 2:

From the Python 2.6.4 documentation, Section 6.2.1. (Augmented 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.

[Emphasis added]

Solution 3:

See in the documentation regarding Emulating numeric types, which describes the methods that implement this behaviour. This also applies to lists:

These methods are called to implement the augmented arithmetic assignments (+=, -=, *=, /=, //=, %=, **=, <<=, >>=, &=, ^=, |=). These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, to execute the statement x += y, where x is an instance of a class that has an __iadd__() method, x.__iadd__(y) is called. If x is an instance of a class that does not define a __iadd__() method, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y.

Solution 4:

When you do list2 += [1] you are modifying the list in place. And that is why you don't change the reference the list points to, but you are changing the list directly. When you do a list2 = list2 + [1], you are creating a new list.

>>>l = []>>>id(l)
41523720L
>>>l += [3]>>>id(l)
41523720L     # same as l = []
>>>l = l+[3]>>>id(l)
41532232L

That explains the difference.

Solution 5:

Well, because that's how it works. When you write list2 = list2 + [1], you create new list and bind it to list2 name. When you use +=, operation happens "in place". So when list1 and list2 references the same object, which is case here, you modify it with += operator.

Post a Comment for "What Was The Motivation For Doing Lists Augmented Assignment (+=) In Place In Python?"