[New-bugs-announce] [issue6730] dict.fromkeys() should not cross reference mutable value by default
Maxime Lemonnier
report at bugs.python.org
Tue Aug 18 22:28:55 CEST 2009
New submission from Maxime Lemonnier <maxime.lemonnier at gmail.com>:
Consider the following code sample :
keys = ['x', 'y', 'z']
d = dict.fromkeys(keys, [])
d['x'].append('dont')
d['y'].append('mix')
d['z'].append('me!')
print d['x']
>>> ['dont', 'mix', 'me!']
It is very unatural and dangerous to have all dict keys poining to the
same mutable object reference.
The way it should behave :
if value is mutable, create a new copy of value for each keys
else, it doesn't matter
----------
components: Interpreter Core
messages: 91714
nosy: maxlem
severity: normal
status: open
title: dict.fromkeys() should not cross reference mutable value by default
type: behavior
versions: Python 2.6
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6730>
_______________________________________
More information about the New-bugs-announce
mailing list