a clean way to define dictionary

Alexander Schmolck a.schmolck at gmx.net
Wed Jun 18 17:34:41 CEST 2003

Alex Martelli <aleax at aleax.it> writes:

> Sean 'Shaleh' Perry wrote:
> > On Tuesday 17 June 2003 21:35, Kendear wrote:
> >>
> >> or is there a more common way to define a dictionary
> >> without all the punctuation marks?
> > 
> >>>> def make_dict(**kwargs):
> > ...     return kwargs
> > ...
> >>>> make_dict(foo=1, bar='sean')
> > {'foo': 1, 'bar': 'sean'}
> > 
> > is the most common form I have seen.  Not sure if it works for you.
> In 2.3, you can express this as dict(foo=1, bar='sean') without a
> need to define a function for the purpose.

Yuck! This seems like a really bad idea to me. This effectively makes it
impossible to specify any options (such as initial size, default value etc.)
at dict creation for dict subclasses or lookalikes or indeed future versions
of dict itself! Not a good tradeoff for minor syntactic sugar, IMO! If this
sugar is really needed, couldn't dict grow an appropriate classmethod instead
(e.g dict.with(foo=1, bar='sean))?


More information about the Python-list mailing list