[Python-3000] the dict constructor

Steven Bethard steven.bethard at gmail.com
Thu Apr 27 20:02:27 CEST 2006


On 4/26/06, Guido van Rossum <guido at python.org> wrote:
> ... the dict constructor signature is
> already too far overloaded, giving a different meaning to keyword
> args, and distinguishing between a mapping and a sequence is iffy: it
> requires a "keys" method which is kind of sub-optimal compared to
> using "iteritems" if it existed...

Yes, this is a pain to emulate, as the code in
UserDict.DictMixin.update shows.  Is there something we can do in
Python 3000 to eliminate the need to check for "keys"?  It bothers me
that the language reference [1] says that to be a mapping type, you
just need to provide __len__, __getitem__, etc. and that "keys",
"values" etc. are just reccommended methods, but yet the
implementation seems to imply otherwise.

I guess maybe we should consider restricting the dict constructor in
Python 3000?

I'd like to keep keyword args if possible -- I find the syntax:
    dict(foo='spam', bar=42)
incredibly convenient.

If dict(), like set(), list() and tuple() just took a sequence
argument, we would break the nice consistency that for most
collections type(obj)(obj) copies the object, e.g.
    set(set_obj) # copies the set_obj
    list(list_obj) # copies the list_obj
I guess that's not horrible given the existence of copy(), but it
makes me uneasy.

If the dict constructor just took a mapping though, you couldn't do
use generator comprehensions with the dict constructor anymore, e.g.
the following would break:
    dict((foo(x), bar(x)) for x in items)
I guess if we went this route, I'd want to push for adding a dict
comprehension syntax to cover this use case.

It's probably clear that I don't have any solutions to this problem,
but I thought I'd bring it up in case anyone out there does.

[1]http://docs.python.org/ref/sequence-types.html

STeVe
--
Grammar am for people who can't think for myself.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list