New submission from Maxime Lemonnier <maxime.lemonn...@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 <rep...@bugs.python.org>
<http://bugs.python.org/issue6730>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to