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

Stefan Behnel stefan_ml at behnel.de
Fri Apr 20 16:49:17 CEST 2012


Alexander Belopolsky, 20.04.2012 16:35:
> On Fri, Apr 20, 2012 at 9:37 AM, Sven Marnach wrote:
>>> 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.)
> 
> >>> a = {}
> >>> b = {1:2}
> >>> dict(a, **b)
> {1: 2}

That's no guaranteed behaviour, though. It doesn't work in PyPy, for example:

>>>> a={}
>>>> b={1:2}
>>>> dict(a,**b)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: keywords must be strings

(and, no, it's not PyPy that's wrong here)

Stefan




More information about the Python-ideas mailing list