[Python-ideas] Adding "+" and "+=" operators to dict

Stephen J. Turnbull stephen at xemacs.org
Sat Feb 14 07:09:40 CET 2015


Chris Barker writes:

 > merging two dicts certainly is _something_ like addition.

But you can't say what that something is with precision, even if you
abstract, and nobody contests that different applications of dicts
have naively *different*, and in implementation incompatible,
"somethings".

So AFAICS you have to fall back on "my editor forces me to type all
the characters, so let's choose the interpretation I use most often
so cI can save some typing without suffering too much cognitive
dissonance."

Following up the off-topic comment:

 > [In numpy,] we really want readable code:
 > 
 > y = a*x**2 + b*x + c
 > 
 > really reads well, but it does create a lot of temporaries that kill
 > performance for large arrays. You can optimize that by hand by doing
 > something like:
 > 
 > y = x**2
 > y *= a
 > y += b*x
 > y += c

Compilers can optimize such things very well, too.  I would think that
a generic optimization to the compiled equivalent of

    try:
        y = x**2p
        y *= a
        y += b*x
        y += c
    except UnimplementedError:
        y = a*x**2 + b*x + c

would be easy to do, possibly controlled by a pragma (I know Guido
doesn't like those, but perhaps an extension PEP 484 "Type Hints"
could help here).



More information about the Python-ideas mailing list