Augument assignment versus regular assignment

Antoon Pardon apardon at forel.vub.ac.be
Mon Jul 10 05:52:22 EDT 2006


On 2006-07-09, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Frank Millman wrote:
>
>> So it looks as if x +=  [] modifies the list in place, while x = x + []
>> creates a new list.
>
> objects can override the += operator (by defining the __iadd__ method), 
> and the list type maps __iadd__ to extend.  other containers may treat 
> += differently, but in-place behaviour is recommended by the language
> reference:
>
>    An augmented assignment expression like x += 1 can be rewritten as
>    x = x + 1 to achieve a similar, but not exactly equal effect.  In
>    the augmented version, x is only evaluated once. Also, when possible,
>    the actual operation is performed in-place, meaning that rather than
>    creating a new object and assigning that to the target, the old object
>    is modified instead.

What does it mean that x is only evaluated once.  I have an avltree module,
with an interface much like a directory.  So I added print statements to
__setitem__ and __getitem__ and then tried the following code.

>>> from avltree import Tree
>>> t=Tree()
>>> t['a'] = 1
__setitem__, key = a
>>> t['a']
__getitem__, key = a
1
>>> t['a'] = t['a'] + 1
__getitem__, key = a
__setitem__, key = a
>>> t['a'] += 1
__getitem__, key = a
__setitem__, key = a
>>> t['b'] = []
__setitem__, key = b
>>> t['b'] = t['b'] + [1]
__getitem__, key = b
__setitem__, key = b
>>> t['b'] += [2]
__getitem__, key = b
__setitem__, key = b

So to me it seems that when we substitute t['a'] or t['b'] for x,
x is evaluated twice with the augmented version, just like it
is with the not augmented version.

-- 
Antoon Pardon




More information about the Python-list mailing list