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: += not assigning to same var it reads
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: chriscog, edloper, fdrake, rhettinger, tim.peters
Priority: high Keywords:

Created on 2001-04-21 23:25 by edloper, last changed 2022-04-10 16:03 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ref6.diff rhettinger, 2002-06-25 04:36 Patch to Ref Manual
Messages (6)
msg4469 - (view) Author: Edward Loper (edloper) * (Python triager) Date: 2001-04-21 23:25
My understanding of the augmented assignment 
statements is that they should always assign to the 
variable that they read from.  However, consider the 
following Python session:
  >>> class A: x = 1  
  ...  
  >>> a=A()
  >>> a.x += 1
  >>> a.x, A.x
  (2, 1)

Here, the expression "a.x += 1" read from a class 
variable, and wrote to an instance variable.  A 
similar effect can occur within a member function:
  >>> class A:
  ...     x = 1
  ...     def f(s): s.x += 1
  ... 
  >>> a = A()
  >>> a.f()
  >>> a.x, A.x
  (2, 1)

I have elicited this behavior in Python 2.0 and 2.1 
under Linux (Redhat 6.0), and Python 2.0 on sunos5.
msg4470 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-04-22 02:46
Logged In: YES 
user_id=31435

Sorry, but your understanding is flawed.  Changed the 
category to Documentation and assigned to Fred, because I 
agree the Ref Man isn't clear enough here:  "The target is 
evaluated only once" is certainly how Guido *thinks* of it, 
but it really needs a by-cases explanation:

For an attributeref target, the primary expression in the 
reference is evaluated only once, but a getattr is done on 
the RHS and a setattr on the LHS.  That is,

    e1.attr op= e2

acts like

    temp1 = e1
    temp2 = e2
    temp1.attr = temp1.attr op temp2

Etc.  It's going to take some real work to write this up so 
they're comprehensible!  I recommend skipping most words 
and presenting "workalike" pseudo-code sequences instead.  
That will take some sessions with the disassembler to be 
sure the pseudo-code is 100% accurate.
msg4471 - (view) Author: Chris Cogdon (chriscog) * Date: 2001-05-04 01:00
Logged In: YES 
user_id=67116

IMHO, this is not a bug. Assignments are always made to the 
class instance, not the class type. X+=1 should be 
equivalent to going X=X+1, and is just so in the above 
cases.
msg4472 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-06-25 04:34
Logged In: YES 
user_id=80475

See attached documentation patch.
msg4473 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2002-06-25 13:23
Logged In: YES 
user_id=3066

Thanks!  Check in as patched.  Does the tutorial need
changes as well?
msg4474 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-06-25 13:40
Logged In: YES 
user_id=80475

Committed as ref6.tex 1.55 and 1.47.4.2.
Closing bug.
History
Date User Action Args
2022-04-10 16:03:59adminsetgithub: 34391
2001-04-21 23:25:51edlopercreate