This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dict.fromkeys()
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: gvanrossum, jvr, rhettinger, theller
Priority: normal Keywords: patch

Created on 2002-11-25 10:29 by rhettinger, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
sequp.diff rhettinger, 2002-11-25 10:29 Patch with code, docs, tests, and news
fromseq.diff rhettinger, 2002-11-26 01:03 Static method version
fromseq2.diff rhettinger, 2002-11-26 08:53 Class method version
fromseq3.diff rhettinger, 2002-11-26 09:29 Class method version with fixed new item
fromseq4.diff rhettinger, 2002-11-26 10:04 Class method version with Just's comments
Messages (14)
msg41753 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-11-25 10:29
Implements dict.sequpdate() as discussed on python-
dev:

def sequpdate(self, iterable, value=True):
    for k in iterable:
        self[k] = value
    return self
msg41754 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2002-11-25 14:09
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}
  >>>
msg41755 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2002-11-25 14:26
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.
msg41756 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-11-26 00:38
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.
msg41757 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-11-26 01:15
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.
msg41758 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-11-26 08:53
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.
msg41759 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2002-11-26 09:13
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)"
msg41760 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-11-26 09:20
Logged In: YES 
user_id=80475

Fixed news item.
msg41761 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2002-11-26 09:40
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.
msg41762 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2002-11-26 09:42
Logged In: YES 
user_id=92689

dang, that should've read "you should NOT incref d at the end".
msg41763 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-11-26 10:04
Logged In: YES 
user_id=80475

Okay, fixed decrefs, eliminated i; used for(;;).
Left in PyObject_CallObject because it's clean.
Thanks.
msg41764 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2002-11-26 10:25
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.
msg41765 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-11-26 16:40
Logged In: YES 
user_id=6380

I'm ok with this, but I haven't had time to review the code.
msg41766 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-11-27 07:36
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

History
Date User Action Args
2022-04-10 16:05:56adminsetgithub: 37531
2002-11-25 10:29:38rhettingercreate