[Python-bugs-list] [ python-Bugs-417930 ] += not assigning to same var it reads

noreply@sourceforge.net noreply@sourceforge.net
Tue, 25 Jun 2002 06:40:27 -0700


Bugs item #417930, was opened at 2001-04-21 18:25
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=417930&group_id=5470

Category: Documentation
Group: None
>Status: Closed
Resolution: Accepted
Priority: 6
Submitted By: Edward Loper (edloper)
Assigned to: Raymond Hettinger (rhettinger)
Summary: += not assigning to same var it reads

Initial Comment:
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.


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2002-06-25 08:40

Message:
Logged In: YES 
user_id=80475

Committed as ref6.tex 1.55 and 1.47.4.2.
Closing bug.

----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2002-06-25 08:23

Message:
Logged In: YES 
user_id=3066

Thanks!  Check in as patched.  Does the tutorial need
changes as well?

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-06-24 23:34

Message:
Logged In: YES 
user_id=80475

See attached documentation patch.

----------------------------------------------------------------------

Comment By: Chris Cogdon (chriscog)
Date: 2001-05-03 20:00

Message:
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.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-04-21 21:46

Message:
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.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=417930&group_id=5470