__iadd__ and fellows missing (Python 2.2)

Ralf Juengling juenglin at informatik.uni-freiburg.de
Mon Apr 22 15:51:45 EDT 2002


"Terry Reedy" <tejarex at yahoo.com> wrote in message news:<VqXt8.28409$%8.2434287 at bin2.nnrp.aus1.giganews.com>...
> "Ralf Juengling" <juenglin at informatik.uni-freiburg.de> wrote in
> message news:xdulmbr51x3.fsf at leto.informatik.uni-freiburg.de...
> > I understand now, that 'i+=1' is no real in-place operation
> 
> Nor is it a false in-place operation :<)
> 
> > but  just a shortcut for 'i=i+1' since int is immutable.
> 
> Correct.  This is true for all immutable types.
> 
> > For the sake of clarity, an 'in-place' operation should really
> > work in-place, don't you think?
> 
> Yes, but +=, etc, are not in-place operations, and are not advertised
> as such.

As I found out today, these operations are advertised as in-place
operations in PEP 203 (see below). 
The discussion on whether in-place operations applied to immutables
should raise a type error or not already has taken place, I guess?

Cheers,
Ralf

[Cut out from PEP 203]

      The proposed patch that adds augmented assignment to Python
      introduces the following new operators:
      
         += -= *= /= %= **= <<= >>= &= ^= |=
      
      They implement the same operator as their normal binary form,
      except that the operation is done `in-place' when the left-hand
      side object supports it, and that the left-hand side is only
      evaluated once.
      
      They truly behave as augmented assignment, in that they perform
      all of the normal load and store operations, in addition to the
      binary operation they are intended to do. So, given the expression:
      
         x += y
      
      The object `x' is loaded, then `y' is added to it, and the
      resulting object is stored back in the original place. The precise
      action performed on the two arguments depends on the type of `x',
      and possibly of `y'.

      The idea behind augmented assignment in Python is that it isn't
      just an easier way to write the common practice of storing the
      result of a binary operation in its left-hand operand, but also a
      way for the left-hand operand in question to know that it should
      operate `on itself', rather than creating a modified copy of
      itself.



More information about the Python-list mailing list