[Python-ideas] Allow using ** twice

Joao S. O. Bueno jsbueno at python.org.br
Thu Jun 6 18:19:34 CEST 2013


On 6 June 2013 13:03, MRAB <python at mrabarnett.plus.com> wrote:
> On 06/06/2013 16:17, Haoyi Li wrote:
>>
>>  > I read `dict1 | dict2` as a mapping that would try dict1 *or* dict2 if
>> the key is not in dict1.
>>
>> Idea:
>>
>> dict1 + dict2 -> dict2 takes priority
>> dict1 | dict2 -> dict1 takes priority
>>
>> Does that make sense to anyone? Both + and | are currently un-used iirc.
>>
> It occurs to me that 'Counter' is dict-like, but already uses both +
> and |.
>
> Would there be any times when you want to merge Counter instances in
> the same manner? It could be confusing if you thought of them as
> dict-like but they didn't behave like dicts...

What if instead of simply checking if a key exists or not, these operators  jsut
operate themselves recursively into the values() ?

It is not all unexpected, as "==" already does that -

so "dct3 = dct1 + dct2" would actually perform:

dct3 = dct1.copy()
for k, v in dct2.items():
    dct3[k] = dct3[k] + v if k in dct3 else v

-In that case, it would make more sense to make use of
"or" instead of "|"  - although other binary logic and aritmetic
operators could do the same.

But that would bring no surprises to the already working-fine logic
of counters.

  js
 -><-


>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas


More information about the Python-ideas mailing list