d = {}; d[0:1] = 1; d[0:1] = 2; print d[:]

Robin Thomas robin.thomas at starmedia.net
Thu Mar 1 04:58:27 CET 2001

Using Python 2.0 on Win32. Am I the only person to be depressed by the 
following behavior now that __getitem__ does the work of __getslice__?

Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
 >>> d = {}
 >>> d[0:1] = 1
 >>> d
{slice(0, 1, None): 1}

And then, for more depression:

 >>> d[0:1] = 2
 >>> d
{slice(0, 1, None): 1, slice(0, 1, None): 2}

And then, for extra extra chagrin:

 >>> print d[0:1]
Traceback (innermost last):
   File "<pyshell#11>", line 1, in ?
KeyError: slice(0, 1, None)

So, questions:

1) Is this behavior considered a bug by the BDFL or the community at large? 
If so, has a fix been conceived? Am I re-opening a long-resolved issue?

2) If we're still open to proposed solutions, which of the following do you 

    a) make slices hash and cmp as their 3-tuple (start,stop,step),
       so that if I accidentally set a slice object as a key,
       I can at least re-set it or get it or del it :)

    b) have dict.__setitem__ expressly reject objects of SliceType
       as keys, raising your favorite in (TypeError, ValueError)

Robin Thomas
StarMedia Network, Inc.
robin.thomas at starmedia.net

More information about the Python-list mailing list