a dictionary from a list

George Sakkis gsakkis at rutgers.edu
Sat Jun 25 15:44:22 CEST 2005

"Roy Smith" <roy at panix.com> wrote:

> I just re-read the documentation on the dict() constructor.  Why does it
> support keyword arguments?
>    dict(foo="bar", baz="blah") ==> {"foo":"bar", "baz"="blah"}
> This smacks of creeping featurism.  Is this actually useful in real code?
> It took me several readings of the doc to understand what this was doing.
> Essentially, it's Perl's bareword syntax, and once I realized that, I
> simultaneously understood what was happening and was revolted that Python
> seems to have picked up one of Perl's most bizarre and confusing features.

The worst thing about this form of the dict constructor it's not the
syntax; I think this becomes obvious after you've seen it once. More
annoying is that it "wastes" keyword arguments that could otherwise be
used to determine the characteristics of the dict. Perhaps the most
common desired feature is setting a default value for the dict, that
would allow for instance:

wordCount = dict(default=0)
wordPos = dict(default=[])
for pos,word in enumerate(text):
    wordCount[word] += 1

Other candidate optional arguments would allow type checking (e.g.
dict(keys=int, values=list)) or performance fine-tuning (e.g.
dict(minsize = 10, maxsize = 10000, average = 200)). I hope the
decision for this form of the constructor is reconsidered for python


More information about the Python-list mailing list