[Python-Dev] PEP 203 Augmented Assignment

Thomas Wouters thomas@xs4all.net
Thu, 27 Jul 2000 22:12:42 +0200

On Thu, Jul 27, 2000 at 10:10:51AM -0700, Ka-Ping Yee wrote:
> On Thu, 27 Jul 2000, Guido van Rossum wrote:

> > 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

a[i:j] += b means exactly the same as:

a[i:j] = a[i:j] + b

Whether it does anything other than raising an exception depends entirely on
the types of a and b ! 

> 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)

That's entirely user-defined. Augmented assignment simply extends Pythons
extremely liberal (my cultural heritage tempts me to say 'communist';)
semantics in these cases.

a[i:j:k] += 1 is exactly

a[i:j:k] = a[i:j:k] + 1

If 'a' is a Python class, this would turn into (forgetting about order of
evaluation, for a second):

a.__setitem__(slice(i, j, k), a[slice(i, j, k)].__add_ab__(1))

> 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

Sure, but it doesn't make sense unless 'a[i:j] = a[i:j] + b' raises similar
problems. Why this arbitrary border ? Because you can't imagine people
wanting it ?

Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!