Josh Rosenberg <shadowranger+pyt...@gmail.com> added the comment:

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.

----------
assignee:  -> docs@python
components: +Documentation
nosy: +docs@python, josh.r

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36715>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to