[Python-ideas] On evaluating features [was: Unpacking iterables for augmented assignment]

Nick Timkovich prometheus235 at gmail.com
Tue Aug 28 14:52:55 EDT 2018


On Tue, Aug 28, 2018 at 12:57 PM Guido van Rossum <guido at python.org> wrote:

> However, a user who doesn't typically think about the actual semantics of
> iterable unpacking and tuple packing might think this would instead mean
> the following:
>
>   a += x
>   b += x
>   c += x
>
> IOW they might think that this is a clever way to increment three
> variables at once:
>
>   a, b, c += 1
>
> This ambiguity (in naive users' minds) leads me to frown upon the proposal.
>

This sort of broadcasting is often quite convenient in Numpy, where many
arithmetic operators will accept scalars or vectors and generally "do the
right thing" in the context. It gets complicated in higher dimensions, but
for 0/1-dimensions you don't need an axis-selector.

    abc = np.array([1, 2, 3])
    abc += 1
    print(abc)
    # [2 3 4]

    abc += [10, 100, 1000]
    print(abc)
    # [  12  103 1004]

The quirks I see are with + being a concatenation operator for lists/tuples.

    a, b = (10, 20)
    # a, b += (1, 2)
    # a, b == 11, 22?
    (a, b) + (1, 2)
    # (10, 20, 1, 2)

Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180828/2aded6ca/attachment.html>


More information about the Python-ideas mailing list