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

Nathan Schneider neatnate at gmail.com
Sat Feb 14 07:23:46 CET 2015


On Sat, Feb 14, 2015 at 1:09 AM, Stephen J. Turnbull <stephen at xemacs.org>
wrote:

> Chris Barker writes:
>
>  > merging two dicts certainly is _something_ like addition.
>
>
So is set union (merging two sets). But in Python, binary + is used mainly
for

  - numerical addition (and element-wise numerical addition, in Counter),
and
  - sequence concatenation.

Being unordered, dicts are more like sets than sequences, so the idea of
supporting dict1 + dict2 but not set1 + set2 makes me queasy.

Cheers,
Nathan



> 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).
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150214/5d0d7d79/attachment.html>


More information about the Python-ideas mailing list