Python 3.x
Use collections.abc.Hashable or typing.Hashable.
>>> import typing
>>> isinstance({}, typing.Hashable)
False
>>> isinstance(0, typing.Hashable)
True
Note: both are the same one, the latter is simply an alias of the former. Also note that collections.Hashable was removed in Python 3.10+ (deprecated since 3.7).
Python 2.6+ (an original answer)
Since Python 2.6 you can use the abstract base class collections.Hashable:
>>> import collections
>>> isinstance({}, collections.Hashable)
False
>>> isinstance(0, collections.Hashable)
True
This approach is also mentioned briefly in the documentation for __hash__.
Doing so means that not only will instances of the class raise an appropriate
TypeErrorwhen a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checkingisinstance(obj, collections.Hashable)(unlike classes which define their own__hash__()to explicitly raiseTypeError).