TypeError: unhashable type

Mark Hammond mhammond at skippinet.com.au
Fri Jun 9 20:54:44 EDT 2000


"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.

> It's a tad tricky to implement because the error message is generated
> in PyObject_Hash.  The error would need to be caught in
> PyDict_SetItem, cleared, and a new one set.

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"??

"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*.

Giving-up-ly,

Mark.





More information about the Python-list mailing list