in place functions from operator module
Peter Otten
__peter__ at web.de
Sun Aug 29 11:00:22 EDT 2010
ernest wrote:
> The operator module provides separate functions for
> "in place" operations, such as iadd(), isub(), etc.
> However, it appears that these functions don't really
> do the operation in place:
>
> In [34]: a = 4
>
> In [35]: operator.iadd(a, 3)
> Out[35]: 7
>
> In [36]: a
> Out[36]: 4
>
> So, what's the point? If you have to make the
> assignment yourself... I don't understand.
Integers are immutable, and for instances a of immutable types
a += b
is equivalent to
a = a + b
For mutable types like list add() and iadd() may differ:
>>> a = ["first"]
>>> operator.iadd(a, [42])
['first', 42]
>>> a
['first', 42]
>>> a = ["first"]
>>> operator.add(a, [42])
['first', 42]
>>> a
['first']
Peter
More information about the Python-list
mailing list