[Python-ideas] Allow using ** twice
Masklinn
masklinn at masklinn.net
Fri Jun 7 10:07:21 CEST 2013
On 2013-06-07, at 09:23 , Stephen J. Turnbull wrote:
> Andrew Barnert writes:
>> On Jun 6, 2013, at 8:43, Haoyi Li <haoyi.sg at gmail.com> wrote:
>
>> There should be one way to do it...I'd agree except, well, there
>> are two different things that people want to do here!
>
>> Actually, there's three:
>
> Four: associate a multivalue containing all of the individual values
> to the key (sort of a set-valued Counter). (This comes up in practice
> in applications that assume UUIDs on the Internet, especially in mail.
> For example, when I receive a post via both a mailing list and a
> personal copy, eventually I want to save the mailing list's version in
> preference to the personal copy. But I only want to display one
> version in the MUA. In archiving, you run into the occasional author
> who provides their own non-unique message ID conflicting with a
> *different* message. Etc.)
>
> And somebody was just asking for Counter addition. Counter is a dict
> subclass, so "dict(...) + dict(...) = updated_dict" would imply really
> perverse semantics for "Counter(...) + Counter(...)".
>
> How about extending .update to take multiple positional arguments?
> Then TOOWTDI idiom would be
>
> {}.update(d1, d2, …)
One of the rather annoying things in dict.update is that it alters the
caller in place, so it can't be used to merge multiple dicts in an
expression. dict(d1, **d2) works to a certain extent, but is not ideal
either.
> The "first found wins" interpretation could use a different method:
>
> {}.extend(d1, d2, …)
IIRC in underscore.js this is called "defaults", d1, d2, … are a
"stack" of applicable defaults, and thus are merged into the
subject if and only if the corresponding keys are missing from
the subject.
More information about the Python-ideas
mailing list