[Python-ideas] Allow using ** twice

MRAB python at mrabarnett.plus.com
Fri Jun 7 00:03:51 CEST 2013


On 06/06/2013 20:41, Ethan Furman wrote:
> On 06/06/2013 12:20 PM, Joao S. O. Bueno wrote:
>> On 6 June 2013 13:43, Ethan Furman wrote:
>>> On 06/06/2013 09:19 AM, Joao S. O. Bueno wrote:
>>>>
>>>> What if instead of simply checking if a key exists or not, these operators
>>>> just operate themselves recursively into the values() ?
>>>>
>>>> It is not all unexpected, as "==" already does that -
>
> I still don't understand this comment on '=='.
>
>
>>>> 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.
>>>
>>>
>>> No surprises?  What about when the function suddenly receives a list when it
>>> wasn't expecting one?  Or did I totally misunderstand?
>>
>> That is no surprises - there won't be a list in there if it was not
>> the programer's intention:
>>
>>>>> dict1["arg1"] = 5
>>>>> dict2["arg1"] = 3
>>>>> dict1 + dict2
>> {"arg1": 8}
>
> This strikes me as very surprising.  And what happens when arg1 is a str? Or some other interesting datatype?
>
> -1 from me.
>
If you wanted to do that you'd use a Counter:

 >>> from collections import Counter
 >>> dict1 = Counter()
 >>> dict2 = Counter()
 >>> dict1["arg1"] = 5
 >>> dict2["arg1"] = 3
 >>> dict1 + dict2
Counter({'arg1': 8})

As for dicts, -1.



More information about the Python-ideas mailing list