[Python-Dev] performance of {} versus dict()
Oleg Broytman
phd at phdru.name
Wed Nov 14 14:25:00 CET 2012
On Thu, Nov 15, 2012 at 12:18:38AM +1100, Chris Angelico <rosuav at gmail.com> wrote:
> On Wed, Nov 14, 2012 at 8:12 PM, Chris Withers <chris at simplistix.co.uk> 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.
On the other had it's more powerful. You can write {'class': 'foo'}
but cannot dict(class='bar'). {1: '1'} but not dict(1='1').
Oleg.
--
Oleg Broytman http://phdru.name/ phd at phdru.name
Programmers don't die, they just GOSUB without RETURN.
More information about the Python-Dev
mailing list