Nick Craig-Wood wrote:

> Which appears to support my point, x (and a for that matter) are the
> same for both methods wheter you do x = x + a or x += a.
>
> The mechanism is different certainly, but the result should be the
> same otherwise you are breaking the basic rules of arithmetic the
> programmer expects (the rule of least suprise).

No - for a mutable object, x may be bound to the original, but
*modified* object after augmented assignment. Any name also bound to
that object will therefore refer to the modified object.

For an immutable object, augmented assignment will not ever modify the
original object.

The two operations may have *very* different effects.

>>> a = [1]
>>> b = [2]
>>> c = a
>>> d = a
>>> c = a + b
>>> a, b, c
([1], [2], [1, 2])
>>> id(a), id(b), id(c)
(11082528, 11271104, 11082328)
>>> a += b
>>> a, b, c
([1, 2], [2], [1, 2])
>>> id(a), id(b), id(c), a is d
(11082528, 11271104, 11082328, True)

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

Reply via email to