Re: [Python-ideas] Adding "+" and "+=" operators to dict
On Sat, Feb 14, 2015 at 1:09 AM, Stephen J. Turnbull <stephen@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@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On 14 February 2015 at 17:23, Nathan Schneider <neatnate@gmail.com> wrote:
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.
But using the same operator as for sets has the same issue as updating dicts is not the same as combining sets either. It behaves unlike any other operation for which we use operators. So, we might quite well use +. -Alexander
participants (2)
-
Alexander Heger
-
Nathan Schneider