[Patches] [ python-Patches-643443 ] dict.fromkeys()

noreply@sourceforge.net noreply@sourceforge.net
Tue, 26 Nov 2002 23:36:15 -0800


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

Category: Core (C code)
Group: Python 2.3
>Status: Closed
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Raymond Hettinger (rhettinger)
>Summary: dict.fromkeys()

Initial Comment:
Implements dict.sequpdate() as discussed on python-
dev:

def sequpdate(self, iterable, value=True):
    for k in iterable:
        self[k] = value
    return self


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

>Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-27 02:36

Message:
Logged In: YES 
user_id=80475

After more code review, added PyDict_Check.

Also, discussion on python-dev showed a need to rename 
the method to indicate that keys were being used rather 
than items.  The sequence part of the name was dropped 
because any iterable will do as an argument.

Checked-in as:
Misc/NEWS 1.548
Objects/dictobject.c 2.132
Lib/test/test_types.py 1.41
Doc/lib/libstdtypes.tex 1.111



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

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-11-26 11:40

Message:
Logged In: YES 
user_id=6380

I'm ok with this, but I haven't had time to review the code.

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

Comment By: Just van Rossum (jvr)
Date: 2002-11-26 05:25

Message:
Logged In: YES 
user_id=92689

Btw. I was looking for more info on METH_CLASS and came
across this thread on python-dev:
 
http://mail.python.org/pipermail/python-dev/2002-April/023566.html
which resulted in this bug report:
  http://www.python.org/sf/548651
which has not been resolve. I'll add a comment there, too.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-26 05:04

Message:
Logged In: YES 
user_id=80475

Okay, fixed decrefs, eliminated i; used for(;;).
Left in PyObject_CallObject because it's clean.
Thanks.

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

Comment By: Just van Rossum (jvr)
Date: 2002-11-26 04:42

Message:
Logged In: YES 
user_id=92689

dang, that should've read "you should NOT incref d at the end".

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

Comment By: Just van Rossum (jvr)
Date: 2002-11-26 04:40

Message:
Logged In: YES 
user_id=92689

- I'm not sure whether PyObject_CallObject() is the right
idiom here. I would have expected to call cls->tp_new (if
not NULL), but I'm not sure either way.
- I would get rid of the loop counter: you're not using it.
for (;;) will do nicely
- you're XDECREF'ing values of which you know they're not NULL
- idem XINCREF. Also: I'm pretty sure you should incref d at
the end: I think it currently leaks.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-26 04:20

Message:
Logged In: YES 
user_id=80475

Fixed news item.

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

Comment By: Thomas Heller (theller)
Date: 2002-11-26 04:13

Message:
Logged In: YES 
user_id=11105

It seems the code and the docs are out of sync in 
fromseq2.diff.

The docs say "fromseq(sequence, value=True)" while the 
code does "fromseq(sequence, value=None)"

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-26 03:53

Message:
Logged In: YES 
user_id=80475

Done!  Revise patch attached.

It is now a class method and uses cls for the type of the 
new instance.  Added tests to demonstrate meaningful 
inheritance.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-11-25 20:15

Message:
Logged In: YES 
user_id=6380

Hm, but the convention for "alternate constructors" is that
the cls argument *is* used to determine the type of the new
instance. This way, the constructors are inherited in a
meaningful way by subclasses of dict.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-25 19:38

Message:
Logged In: YES 
user_id=80475

Attached revised patch incorporating all of the review 
comments.  Changed default to None.  Made a static 
method instead of a classmethod because cls was not 
used.  Doesn't return self.

Tests, docs, and news item revised accordingly.

Ready for BDFL pronouncement.

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

Comment By: Just van Rossum (jvr)
Date: 2002-11-25 09:26

Message:
Logged In: YES 
user_id=92689

One more thing: I'd say the default value should be None.
This makes the idiom also useful to initialize dicts which
later get filled with proper values. This doesn't take
anything away from your original use case.

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

Comment By: Just van Rossum (jvr)
Date: 2002-11-25 09:09

Message:
Logged In: YES 
user_id=92689

I see no reason why d.sequpdate() should return self.
d.update() doesn't either.

Also, as I suggested on python-dev, a fromseq() constructor
(as a class method) would be nice:

  >>> dict.fromseq('End Quit Stop Abort'.split())
  {'End': True, 'Quit': True, 'Stop': True, 'Abort': True}
  >>>

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

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