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.

Author josh.r
Recipients Aditya Sane, docs@python, josh.r
Date 2019-04-24.20:13:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1556136814.37.0.335903300218.issue36715@roundup.psfhosted.org>
In-reply-to
Content
This is the same basic problem seen with sequence multiplication, mutable default arguments to a function, etc. It's not going to change though; the documentation says it "Create a new dictionary with keys from iterable and values set to value." "set to value" doesn't allow for implicit copy operations (shallow or deep), and trying to support it would slow fromkeys and violate the normal expectations for functions that reuse an input argument. Essentially, the problem you have is one of expectations; the behavior is correct, but perhaps the documentation could be clearer.

In any event, the general solution to your problem is a dict comprehension (or for dict subclasses, passing a generator expression to the subclass constructor), replacing the incorrect:

    dict.fromkeys(iterable, [])

with the similarly concise/straightforward:

    {k: [] for k in iterable}

or (for general use, where dict can be changed to a dict subclass):

    dict((k, []) for k in iterable)

I've marked this as a documentation bug if someone wants to take a stab at making the behavior more clear.
History
Date User Action Args
2019-04-24 20:13:34josh.rsetrecipients: + josh.r, docs@python, Aditya Sane
2019-04-24 20:13:34josh.rsetmessageid: <1556136814.37.0.335903300218.issue36715@roundup.psfhosted.org>
2019-04-24 20:13:34josh.rlinkissue36715 messages
2019-04-24 20:13:34josh.rcreate