[Python-Dev] Dict constructor
Raymond Hettinger
python@rcn.com
Wed, 26 Jun 2002 15:45:09 -0400
From: "Tim Peters" <tim@zope.com>
> -1 because of ambiguity. Is this trying to build a set with the single
> element (42, 666), or a mapping of 42 to 666?
>
> dict([(42, 666)]}
I've been thinking about this and the unabiguous explicit solution is to
specify a value argument like dict.get().
>>> dict([(42, 666)]) # current behavior unchanged
{42: 666}
>>> dict([(42, 666)], True)
{(42, 666): True}
>>> dict( '0123456789abcdef', True)
{'a': True, 'c': True, 'b': True, 'e': True, 'd': True, 'f': True, '1':
True, '0': True, '3': True, '2': True, '5': True, '4': True, 7': True, '6':
True, '9': True, '8': True}
>>> dict('0123456789abcdef') # current behavior unchanged
ValueError: dictionary update sequence element #0 has length 1; 2 is
required
The goal is not to provide full set behavior but to facilitate the common
task of building dictionaries with a constant value. It comes up in
membership testing and in uniquifying sequences. The task of dict() is to
construct dictionaries and this is a reasonably common construction.
Raymond Hettinger