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

R. David Murray rdmurray at bitdance.com
Wed Nov 14 18:27:46 CET 2012


On Wed, 14 Nov 2012 11:10:15 -0600, Mark Adam <dreamingforward at gmail.com> wrote:
> On Wed, Nov 14, 2012 at 11:00 AM, Brian Curtin <brian at python.org> wrote:
> > On Wed, Nov 14, 2012 at 10:12 AM, Mark Adam <dreamingforward at gmail.com> wrote:
> >> On Wed, Nov 14, 2012 at 3:12 AM, Chris Withers <chris at simplistix.co.uk> wrote:
> >>> Hi All,
> >>>
> >>> A colleague pointed me at Doug's excellent article here:
> >>> ...which made me a little sad, 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,
> >>>     ...
> >>>     }
> >>
> >> Hey, it makes me a little sad that dict breaks convention by allowing
> >> the use of unquoted characters (which everywhere else looks like
> >> variable names) just for a silly typing optimization.
> >
> > What convention and typing optimization is this? I hope you aren't
> > suggesting it should be dict("x"=1) or dict("x":1)?
> 
> Try the canonical {'x':1}.  Only dict allows the special
> initialization above.  Other collections require an iterable.  I'm guessing
> **kwargs initialization was only used because it is so simple to
> implement, but that's not necessarily a heuristic for good language design.

Maybe it's not good design, but I'll bet you that if it didn't do that,
there would be lots of instances of this scattered around various
codebases:

    def makedict(**kw):
        return kw

--David


More information about the Python-Dev mailing list