[Python-Dev] performance of {} versus dict()

Lukas Lueg lukas.lueg at gmail.com
Thu Nov 15 00:38:38 CET 2012


Notice that {'x':1} and dict(x=1) are different beasts: The first one
compiles directly to BUILD_MAP. The second one loads a reference to 'dict'
from globals() and calls the constructor. The two are not the same.



2012/11/15 Steven D'Aprano <steve at pearwood.info>

> On 15/11/12 05:54, Mark Adam wrote:
>
>  Merging of two dicts is done with dict.update.   How do you do it on
>> initialization?  This doesn't make sense.
>>
>
> Frequently.
>
> my_prefs = dict(default_prefs, setting=True, another_setting=False)
>
>
> Notice that I'm not merging one dict into another, but merging two dicts
> into a third.
>
> (Well, technically, one of the two comes from keyword arguments rather
> than an actual dict, but the principle is the same.)
>
> The Python 1.5 alternative was:
>
> my_prefs = {}
> my_prefs.update(default_prefs)
> my_prefs['setting'] = True
> my_prefs['another_setting'] = False
>
>
> Blah, I'm so glad I don't have to write Python 1.5 code any more. Even
> using copy only saves a line:
>
> my_prefs = default_prefs.copy()
> my_prefs['setting'] = True
> my_prefs['another_setting'] = False
>
>
>
>
> --
> Steven
>
> ______________________________**_________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/**mailman/listinfo/python-dev<http://mail.python.org/mailman/listinfo/python-dev>
> Unsubscribe: http://mail.python.org/**mailman/options/python-dev/**
> lukas.lueg%40gmail.com<http://mail.python.org/mailman/options/python-dev/lukas.lueg%40gmail.com>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20121115/8316bda0/attachment.html>


More information about the Python-Dev mailing list