[Python-Dev] dict() enhancement idea?

Just van Rossum just@letterror.com
Thu, 21 Nov 2002 13:51:15 +0100

I sometimes use an idiom like

  def dictfromkeywords(**kwargs):
      return kwargs

  d = dictfromkeywords(
          akey = 12,
          anotherkey = "foo",

to conveniently build dicts with literal keys. I think I've seen Alex Martelli
advertise it, too. Don't know how well known it is otherwise, but it is extremly

It just occured to me that the dict constructor could easily be overloaded with
this behavior: it currently takes no keyword arguments[*], so the kwargs dict
could simply be used to initialize the new dict.

Has this been proposed before? Can't seem to find any reference to it.

I could try to make a patch if people think this is a good idea.

[*] caveat: dict_new() currently _does_ take one keyword argument "items", using
it is the same as simply passing one arg to dict(). It seems to suggest a (key,
value) list, though, but strangely it also works for dicts:

  >>> d = {1: 2, 3: 4}
  >>> dict(items=d)
  {1: 2, 3: 4}

However, this "items" keyword arg is not documented, and together with the above
oddity I suggest to simply get rid of it.