Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What was the motivation for doing lists augmented assignment (+=) in place in python? [duplicate]

Tags:

python

>>> list1 = []
>>> list2 = list1
>>> list2 += [1]
>>> print list1
[1]

Comparing this to

>>> list1 = []
>>> list2 = list1
>>> list2 = list2 + [1]
>>> print list1
[]

Is there a reason why the '+='-operation modifies the original list?

EDIT: just to make my question a bit clearer

In most of the languages that I know the '+='-operator doesn't work this way and I would like to know why it was designed in this way in python.

some examples:

Ruby

irb(main):001:0> l = []
irb(main):002:0> a = l
irb(main):003:0> a += [2]
irb(main):004:0> l
=> []

Scala etc..

like image 522
mkorpela Avatar asked Mar 09 '11 08:03

mkorpela


People also ask

What is an augmented assignment in Python?

An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is x += 1 which is expanded to x = x + (1) .

What does += do to a list in Python?

The operators + and * can be used to concatenate lists and multiply lists. The compound operators += and *= can concatenate lists and multiply lists and pass the new identity to the original list.

Is %= an augmented assignment operator?

The means that += , -= , *= , /= , //= , %= , **= , >>= , <<= , &= , ^= , and |= are all assignment operators. We can see obvious parallels between sums using += , and products using *= . In the case of mutable objects, this augmented assignment can take on special ...

What is the assignment operator and what is an augmented assignment operator?

Assignment Operator is nothing but the "equal to - =" operator. This operator assigns a value to a variable. Augmented Assignment Operators performs operations and assigns the value to the same variable.


2 Answers

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.

like image 184
Amber Avatar answered Nov 15 '22 00:11

Amber


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]

like image 20
Oddthinking Avatar answered Nov 14 '22 22:11

Oddthinking