[ python-Bugs-1733184 ] slice type is unhashable

SourceForge.net noreply at sourceforge.net
Fri Jun 8 07:36:09 CEST 2007


Bugs item #1733184, was opened at 2007-06-07 19:30
Message generated for change (Comment added) made by lpd
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1733184&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: L. Peter Deutsch (lpd)
Assigned to: Nobody/Anonymous (nobody)
Summary: slice type is unhashable

Initial Comment:
Patch # 408326 is a "bug fix" that makes slice objects comparable but explicitly NOT hashable.  I don't understand why Guido thinks this is the right behavior for them: they are immutable, have well-defined state, do not include references to mutable objects, and can be compared property for equality.  Why shouldn't they be usable as dictionary keys?  I have an application that really would like them to be usable as such.  I know I can define a class Slice of my own .. but that seems so silly.

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

>Comment By: L. Peter Deutsch (lpd)
Date: 2007-06-07 22:36

Message:
Logged In: YES 
user_id=8861
Originator: YES

I could probably live with that.  However, there are other kinds of
objects being used as keys in the same dictionary, and I can't just repr()
them all, because it is not true that a == b implies repr(a) == repr(b). 
(E.g., a = 3, b = 3.0.)

Practicalities aside, it just seems silly to me that a simple type like
slice should have all the prerequisites for being hashable and yet isn't,
especially when much more complex types like methods are both comparable
and hashable.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2007-06-07 22:24

Message:
Logged In: YES 
user_id=80475
Originator: NO

FWIW, you have a easy work-around.  Use repr(yourslice) as the dictionary
key.


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

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


More information about the Python-bugs-list mailing list