Peter Otten wrote:
ssecorp wrote:

h = "aja baja"
h += 'e'
h
'aja bajae'

The inplace-add operator doesn't mutate the lvalue, it just rebinds it:

In Python, neither '=' nor members of the 'op=' family are operators.
They all mark *assignment* or *augmented assignment* statements that *all* bind objects to targets.

Augmented assignments are, rather obviously, restricted to binding one object to one target. For 'x op= y', if the object originally bound to x is mutable, the arithmetic operation part of the augmented assignment can (should) be implemented by an inplace __i<opname>__ special method that (normally, but not necessarily) mutates and returns self to be rebound. Otherwise, the interpreter calls the normal __<opname>__ special method (if it exits) that returns a new object to be bound.

Thus, '+=' is neither an operator nor is the indicated operation necessarily inplace.

Immutable built-in classes do not have __i<opname>__ methods. So given that name h is bound to a string,
  h += 'e'
has exactly the same effect as
  h = h + 'e'
which has exactly the same effect as
  h = h.__add__('e')
The same is true for immutable instances of other built-in classes.


Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to