[issue6730] dict.fromkeys() should not cross reference mutable value by default
Georg Brandl
report at bugs.python.org
Tue Aug 25 00:11:54 CEST 2009
Georg Brandl <georg at python.org> added the comment:
I'll add a bit of explanation as well:
> I have to add this ugly loop :
>
> for key in keys:
> d[key] = []
With a defaultdict, you don't -- you just use d[key], and if not already
present, the entry will be initialized with an empty list.
> I really think that :
> 1) The doc should warn about it, since it is a very weird behaviour
It is not weird in Python. Implicit copies are never made, partly
because it's very hard to implement it correctly for every object.
> 2) There could at least be a third argument allowing the user to choose,
> with a default value that maintains the current behaviour (for
> compatibility, even though I think the default should be a deep copy)
The interface of a fundamental type like "dict" will not be changed
lightly. Usually, when a change is made, it is to add a feature that is
often requested by lots of users -- I've never seen someone request this
before. And as we've seen, it can easily be written either using a
simple loop, or a defaultdict, depending on the exact use-case.
----------
nosy: +georg.brandl
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6730>
_______________________________________
More information about the Python-bugs-list
mailing list