[issue6730] dict.fromkeys() should not cross reference mutable value by default

2009-08-18 Thread Maxime Lemonnier
New submission from Maxime Lemonnier : 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

[issue6730] dict.fromkeys() should not cross reference mutable value by default

2009-08-18 Thread Benjamin Peterson
Benjamin Peterson added the comment: This behavior will never change. Use collections.defaultdict for your case. -- nosy: +benjamin.peterson resolution: -> wont fix status: open -> closed ___ Python tracker __

[issue6730] dict.fromkeys() should not cross reference mutable value by default

2009-08-18 Thread Raymond Hettinger
Raymond Hettinger added the comment: I concur. -- nosy: +rhettinger ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscri

[issue6730] dict.fromkeys() should not cross reference mutable value by default

2009-08-18 Thread Maxime Lemonnier
Maxime Lemonnier added the comment: It does not suits my needs. what if keys are passed as an argument? I have to add this ugly loop : for key in keys: d[key] = [] I really think that : 1) The doc should warn about it, since it is a very weird behaviour 2) There could at least be a

[issue6730] dict.fromkeys() should not cross reference mutable value by default

2009-08-18 Thread R. David Murray
R. David Murray added the comment: The docs very clearly say the second argument is the value that will be assigned to the keys. It doesn't matter whether or not that object is mutable, it is that object that gets assigned. This is just the way that Python works: >>> a = b = [] >>> a.append(1

[issue6730] dict.fromkeys() should not cross reference mutable value by default

2009-08-24 Thread Georg Brandl
Georg Brandl 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