in place functions from operator module

ernest nfdisco at gmail.com
Sun Aug 29 11:29:11 EDT 2010


On 29 Ago, 17:00, Peter Otten <__pete... at web.de> wrote:
> 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']

It makes sense now. Thank you :)

Ernest



More information about the Python-list mailing list