Augmented Assignement (was: Re: PEP scepticism)

Tim Peters tim.one at home.com
Sat Jun 30 21:52:30 EDT 2001


[Rainer Deyke, on augmented assignments]
> ... If anything, I would go the other direction and make it illegal
> for mutable types.  In my experience, the primary use of augmented
> assignment is for integers:
>
> a += 1
>
> vs.
>
> a = a + 1
>
> The augmented assignment version is clearly more maintainable since it
> contains less redundancy.  On the other hand, augmented assignment is
> redundant for mutable types.  If I want to extend a list in
> place, I'll use the 'extend' method since it more clearly communicates
> my intent.

OTOH, if you're slinging mega-element conformable arrays x and y,

    x += y

is much more efficient than

    x = x + y  # creates a giant temp array

and much clearer than

    x.add_in_place(y)

It's no coincidence that the most motivated constituency for augmented
assignment on mutable types was the NumPy camp.

As usual in this debate, the participants aren't *themselves* confused about
it, they're worried about people much feebler than they are.  In this
particular case, don't be so sure that isn't an empty set <wink>.





More information about the Python-list mailing list