TypeError: unhashable type
Jeremy Hylton
jeremy at beopen.com
Tue Jun 13 11:14:10 EDT 2000
"Mark Hammond" <mhammond at skippinet.com.au> writes:
> "Jeremy Hylton" <jeremy at beopen.com> wrote in message
> news:F8476B9163F138C3.8DBC9B4CCE38EBEA.9FFEEE60294594AF at lp.airnews.net...
>
> > It seems that "unhashable type" is a pretty obscure error message. If
> > you understand hashes, types, and dictionary implementation, it's
> > fine. If not, you're left scratch your head.
>
> For the casual user, this generally means "dictionary keys must be
> immutable".
>
> > It would be helpful to produce an error message like:
> > "dictionary key must be hashable type, not <type>"
>
> This message isnt a real lot better. Often I _know_ what the type is -
> its just "unhashable" - which still doesnt clue the user in. The word
> "hashable" is a bigger problem than not printing the type.
I was hoping the new error message would provide a few more clues by
mentioning "hashable" and "dictionary key." If the documentation for
dictionaries defined hashable and explained its significant, then a
user would have an easier time figuring out what went wrong.
I agree that "hashable" is puzzling, but I think it is doubly so when
you don't know its related to dictionary keys.
Printing the type of the object is probably of secondary importance.
> I think the situation could still be improved without the type name. But
> for the life of me I can't think how.
>
> "dictionary keys must be immutable" isnt really any better - WTF is
> "immutable"??
Yeah, immutable isn't any better. In fact, it's bad because you can
write a mutable instance that has __hash__ and __cmp__ methods and use
it as a dictionary key.
> "dictionary keys must be objects unable to be changed" - WTF - I can
> change strings!!? Or maybe this is trying to tell me that once I add a
> key, it can't be removed?
>
> Maybe something like:
> "dictionary keys must be immutable (such as tuples, strings or ints, but
> not objects such as lists)" - *gag*.
>
The right goal is probably a slightly clearer error message and
documentation tied specifically to that error message. This matches a
general strategy I use when some piece of software spits out an
obscure error message. I type the exact message into Google,
DejaNews, or AltaVista and see what comes up. More often than not, I
find someone has already answered my question.
Jeremy
More information about the Python-list
mailing list