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

Stefan Behnel stefan_ml at behnel.de
Wed Nov 14 15:35:37 CET 2012


Chris Angelico, 14.11.2012 14:18:
> On Wed, Nov 14, 2012 at 8:12 PM, Chris Withers wrote:
>> I suspect I'm not the only one who finds:
>>>> a_dict = dict(
>> x = 1,
>> y = 2,
>> z = 3,
>> ...
>> )
>>>> ...easier to read than:
>>>> a_dict = {
>> 'x':1,
>> 'y':2,
>> 'z':3,
>> ...
>> }
>>>> What can we do to speed up the former case?
>> Perhaps an alternative question: What can be done to make the latter
> less unpalatable? I personally prefer dict literal syntax to a dict
> constructor call, but no doubt there are a number of people who feel
> as you do. In what way(s) do you find the literal syntax less
> readable, and can some simple (and backward-compatible) enhancements
> help that?
>> I've seen criticisms (though I don't recall where) of Python,
> comparing it to JavaScript/ECMAScript, that complain of the need to
> quote the keys. IMO this is a worthwhile downside, as it allows you to
> use variables as the keys, rather than requiring (effectively) literal
> strings. But it does make a dict literal that much more "noisy" than
> the constructor.

If that bothers you in a specific case, I recommend using the constructor
instead of a literal.
Stefan


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /