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

Erik Bray erik.m.bray at gmail.com
Sat Feb 14 00:29:53 CET 2015


On Fri, Feb 13, 2015 at 6:22 PM, Nikolaus Rath <Nikolaus at rath.org> wrote:
> Alexander Heger <python-ePO413wvQzY at public.gmane.org> writes:
>>> As far as I was, and still am, concerned, & is the obvious and most
>>> natural operator for concatenation. [1, 2]+[3, 4] should return [4, 6],
>>> and sum(bunch of lists) should be a meaningless operation, like
>>> sum(bunch of HTTP servers). Or sum(bunch of dicts).
>>
>> This only works if the items *can* be added.  Dics should not make
>> such an assumption. & is not a more natural operator, because, why
>> would you then not just expect that [1, 2] & [3, 4] returns [1 & 2, 3
>> & 4] == [0 , 0] ?  the same would be true for any operator you pick.
>
> Which brings us back to the idea to introduce elementwise variants of
> any operator:
>
> [1,2] .+ [3,4] == [1+3, 2+4]
> [1,2] + [3,4] == [1,2,3,4]
> [1,2] .& [3,4] == [1 & 3, 2 & 4]
> [1,2] & [3,4] == not (yet?) defined
>
> As a regular numpy user, I'd be very happy about that too :-).

... except for the part where in Numpy ".+" and "+" and so one would
have to be identical, which would be no end of confusing especially
when adding, say, a Numpy array and a list.

But I agree in principle it would be nice to have element-wise
operators in the language.  I just fear it may be too late.

Erik


More information about the Python-ideas mailing list