[Python-Dev] Dict constructor

Steve Holden sholden@holdenweb.com
Thu, 27 Jun 2002 07:15:32 -0400


----- Original Message -----
From: "Raymond Hettinger" <python@rcn.com>
To: <python-dev@python.org>
Sent: Wednesday, June 26, 2002 3:45 PM
Subject: Re: [Python-Dev] Dict constructor


> 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.
>
But is it really common enough to merit special-casing what can anyway be
spelt very simply:

adict = {}
for k in asequence:
    dict[k] = sentinel

?
regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------