This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: More clarity needed about difference between "x += e" and "x = x + e"
Type: enhancement Stage:
Components: Documentation Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Kluzniak, docs@python, josh.r, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2014-05-12 16:58 by Kluzniak, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg218338 - (view) Author: Feliks (Kluzniak) Date: 2014-05-12 16:58
In Sec. 7.2.1 of the Language Reference, in the description of "+=" we have: "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."
Although this is quite accurate, not all the consequences are immediately obvious, and sometimes they are quite serious.  I would suggest adding a note that points the reader's attention in particular to the phenomenon exhibited in the following example:
>>> def f(ls):  ls += [2]
... 
>>> def g(ls):  ls = ls + [2]
... 
>>> a = [1]
>>> g(a)
>>> a
[1]
>>> f(a)
>>> a
[1, 2]
msg218339 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2014-05-12 17:11
It seems to me like that is one of the most obvious consequences. How is this not an immediately obvious consequence?
msg218429 - (view) Author: Feliks (Kluzniak) Date: 2014-05-13 10:25
Well, there is some anecdotal evidence. ;-)  I happen to have a lot of experience with a lot of programming languages, and I was bitten by this.

Let's put it like this: it is quite easy to overlook the significance of the sentence in question.  One looks at the paragraph, reads the stuff about evaluation, thinks "of course" and moves on.

If the sentence were in a new paragraph, and instead of with "Also" began with something like "More importantly", then it might have drawn more attention.

After all, the drastic semantic difference between the two forms is quite unlike what one is used to from programming in, say, C.  I think it would be helpful if it were stressed a little bit more.
msg218812 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-05-19 19:57
I agree with Josh.  If anything this belongs in a wiki entry, faq page, or stack overflow question.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65683
2014-05-19 19:57:23rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg218812

resolution: rejected
2014-05-16 18:58:22terry.reedysetnosy: + terry.reedy
2014-05-13 10:25:02Kluzniaksetmessages: + msg218429
2014-05-12 17:11:22josh.rsetnosy: + josh.r
messages: + msg218339
2014-05-12 16:58:01Kluzniakcreate