[Python-ideas] Have dict().update() return its own reference.

Masklinn masklinn at masklinn.net
Fri Apr 20 16:56:04 CEST 2012


On 2012-04-20, at 16:28 , Stefan Behnel wrote:

> Sven Marnach, 20.04.2012 15:37:
>> Masklinn schrieb am Fri, 20. Apr 2012, um 14:47:34 +0200:
>>> If you start from dict instances, you could always use:
>>> 
>>>    merged = dict(x, **y)
>> 
>> No, not always.  Only if all keys of `y` are strings (and probably
>> they should also be valid Python identifiers.)
> 
> Also, it's not immediately clear from the expression what happens for
> duplicate keys

Not sure why, as with `dict.update` `dict` is defined as setting from the
first argument, then setting from the keyword arguments (overriding
keys originally set if any).

Now of course that might not be obvious to people who don't know how dict
works, but I fail to see why an other function which they don't know either
will be any more "immediately clear".

You may counter that a function taking (and merging) a sequence of mappings
would "obviously" apply a left fold in merging the mappings, but in that
case the dict constructor would "obviously" copy the positional then apply
the keywords (which are after the positional).

Which is exactly what happens.



More information about the Python-ideas mailing list