Pre-PEP: __hashable__

Delaney, Timothy tdelaney at avaya.com
Tue Dec 10 21:53:42 EST 2002


> From: Delaney, Timothy [mailto:tdelaney at avaya.com]
> 

Possibly a more reasonable implementation would be:

    def hashable (obj):
 
         try:
             obj.__hash__()
             return obj
         except AttributeError:
             try:
                 return obj.__hashable__()
             except AttributeError:
                 raise TypeError('unhashable type')
 
     def hash (obj):
         try:
             return obj.__hash__()
         except AttributeError:
             try:
                 return hashable(obj).__hash__()
             except AttributeError:
                 raise TypeError('unhashable type')

This would mean in each case that __hash__() is only called the once. Since
__hashable__ is specified to return an object which is hashable anything
which fails to do so is broken.

Tim Delaney




More information about the Python-list mailing list