[Patches] [ python-Patches-642500 ] Allow keyword args in dict() constructor

noreply@sourceforge.net noreply@sourceforge.net
Sat, 23 Nov 2002 01:49:38 -0800


Patches item #642500, was opened at 2002-11-22 21:11
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=642500&group_id=5470

Category: Core (C code)
Group: Python 2.3
>Status: Closed
Resolution: Accepted
Priority: 5
Submitted By: Just van Rossum (jvr)
Assigned to: Just van Rossum (jvr)
Summary: Allow keyword args in dict() constructor

Initial Comment:
I sometimes use an idiom like

  def dictfromkeywords(**kwargs):
      return kwargs

  d = dictfromkeywords(
          akey = 12,
          anotherkey = "foo",
          ...etc.
  )

to conveniently build dicts with literal keys. This
patch adds this behavior to the dict constructor so one
can write:

  dict(one=1, two=2)

The patch patches Doc/lib/libfuncs.text,
Objects/dictobject.c, Lib/test/test_descr.py and Misc/NEWS

The dictobject.c patch contains an XXX comment about
something I'm not 100% sure about. Please review.

Thanks to Raymond H. for earlier comments.

----------------------------------------------------------------------

>Comment By: Just van Rossum (jvr)
Date: 2002-11-23 10:49

Message:
Logged In: YES 
user_id=92689

Ok, I've implemented your suggestions, apart from the
example: I directly translated {1: 2, 2: 3} to {"one": 2,
"two": 3} and the fact that it looks odd perhaps grabs the
readers' attention... Feel free to change it, though.

Checked in as these revisions:
Doc/lib/libfuncs.tex rev 1.121
Lib/test/test_descr.py rev 1.160
Misc/NEWS rev 1.536
Objects/dictobject.c rev 2.131

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-23 02:47

Message:
Logged In: YES 
user_id=80475

Looks good.
Passes review.
Marking as accepted.

Here are some nits before committing:

-- Replace 'elimated' with 'eliminated' in the NEWS entry.

-- I rethought the wording in dictobject.c.  Consider 
replacing "name/value pairs" with "name=value pairs".

-- In libfuncs.tex, replace "Else" with "Otherwise".

-- The tex markup is almost fine, but add a blank line to 
trigger a paragraph break between the paragraph ending 
with "dictionary." and the one starting with "If keyword 
arguments".

-- The {"two":3} example has an oxymoron quality that 
unnecessarily distracts from the message.  Replace it with 
something less confusing and suggesting a legitimate use, 
such as:  {"red":1,"blue:2}.

-- The test suite looks perfect to the eye.  Consider running 
a whitespace normalization just to be sure.


----------------------------------------------------------------------

Comment By: Just van Rossum (jvr)
Date: 2002-11-22 21:29

Message:
Logged In: YES 
user_id=92689

Oops, the doctobject.c *doesn't* contain an XXX comment, I
was thinking of an earlier version of the patch. As far as I
can see the patch is fine as is (although Fred may want to
double check the libfuncs.tex patch).

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=642500&group_id=5470