[Python-bugs-list] [ python-Bugs-710197 ] initialisation problem with dict.fromkeys()

SourceForge.net noreply@sourceforge.net
Wed, 26 Mar 2003 09:59:24 -0800


Bugs item #710197, was opened at 2003-03-26 17:59
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=710197&group_id=5470

Category: None
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Frederic Sohm (fsms)
Assigned to: Nobody/Anonymous (nobody)
Summary: initialisation problem with dict.fromkeys()

Initial Comment:
I have a problem of initialization of dictionary with the new method  
dict.fromkeys(sequence [, defaults]) when defaults is a list.  
  
here an example :  
  
Python 2.3a2 (#1, Feb 21 2003, 14:28:59)   
[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux2  
Type "copyright", "credits" or "license" for more information.  
IDLEfork 0.9a2  
>>> a = {}.fromkeys([0, 1], [ ])  
>>> a[0]  
[ ]  
>>> a[1]  
[ ]  
>>> a[0] += [2]  
>>> a[0]  
[2]  
>>> a[1]  
[2]  
>>> a[1] is a[0]  
True  
>>> a[0].append(3)  
>>> a[0]  
[2, 3]  
>>> a[1]  
[2, 3]  
etc... same thing if I use list() instead of [ ] 
  
the same list seems to be assigned to all the keys.  
when using string, tuple... it is normal (or what I would expect at 
least) : 
>>> b = {}.fromkeys([0,1], '') 
>>> b[0] 
'' 
>>> b[1] 
'' 
>>> b[0] += '2' 
>>> b[0] 
'2' 
>>> b[1] 
'' 
>>>b[0] is b[1] 
False 

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=710197&group_id=5470