[Python-Dev] PEP 203 Augmented Assignment

Ka-Ping Yee ping@lfw.org
Thu, 27 Jul 2000 10:10:51 -0700 (PDT)

On Thu, 27 Jul 2000, Guido van Rossum wrote:
> Second, what should happen to a slice assignment?

My vote is: nothing!  (Read on.)

> The basic slice form is:
>   a[i:j] += b

What does this mean?  I don't see how it could be any different from:

    a[j:j] = b

If people want to insert sequences into other sequences, they should
use this form; if they want to insert an element into a sequence,
they should be encouraged to use an .insert() method.  In other words,
we already have perfectly good ways of doing this (more importantly,
there are already clear and simple ways to implement such behaviour in
your own objects).

>   a[:, ..., ::, 0:10:2, :10:, 1, 2:, ::-1] += 1

What on earth could this mean?  And why would anyone want it?

I can't even imagine what the simpler case

    a[i:j:k] += 1

would do... unless you wish to propose element-wise operators (e.g.
add 1 to each of the elements that would participate in the slice)
and i think that might be a whole new can of worms.  It wasn't clear
to me that this was your intent, though.

It looks to me like going through various contortions to support
augmented assignment to slices is not going to be worth the trouble.

May i suggest

    >>> a[i:j] += b
    SyntaxError: augmented assignment to a slice is not allowed

just like

    >>> (a, b, c) += 1,2,3
    SyntaxError: augmented assignment to a tuple is not allowed


-- ?!ng