[Tutor] Is there a test for hashability?

Steven D'Aprano steve at pearwood.info
Fri Sep 2 04:17:48 CEST 2011


Richard D. Moores wrote:
> Thanks, James, from your ideas I've come up with this function as a
> general test for hashibility of any object:
> 
> def is_hashable(object):
>     try:
>         if hash(object):
>             return True
>     except TypeError:
>         return False

No need for the "if hash" test, just try hash:

def is_hashable(obj):
     try:
         hash(object)
         return True
     except TypeError:
         return False


-- 
Steven


More information about the Tutor mailing list