Unfortunate exception on dict item assignment (and why aren't slices hashable?)

Anders J. Munch andersjm at dancontrol.dk
Tue Jul 29 03:51:44 EDT 2003


"Aahz" <aahz at pythoncraft.com> wrote:
> Jeff Epler  <jepler at unpythonic.net> wrote:
> >
> >I don't see what's problematic about letting slices be hashable: 
> [...]
>Hashing them as
> >    hash((s.start,s.stop,s.step))
> >might be a reasonable definition.
> 
> Makes sense to me.  Feel free to submit a patch to SF.  ;-)

I'd rather he didn't.  I prefer Python to be able to catch the bug of
attempting to use a dict as were it a list.  For the rare case where
using a slice key was intentional, converting to tuple form is easy
enough.

I can see that it's good for consistency to allow hashing of slices.
However it's bad for consistency that where alist[slice] returns a
subsequence, adict[slice] would return a single element.

- Anders






More information about the Python-list mailing list