[Python-3000] callable()

Nick Coghlan ncoghlan at gmail.com
Thu Jul 27 12:36:36 CEST 2006


Guido van Rossum wrote:
> On 7/26/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>    class Unhashable(object):
>>        __hash__ = Undefined
> 
> Simpler: set it to None. That could be a convention.
> 

And someone else pointed out that this could still be a trigger for NULL'ing 
out slots at the C level, since None is a compiler-enforced singleton.

A direct check for callability/hashability/whateverability gets more verbose 
though:

   getattr(obj, "__hash__", None) is not None

The upside is that it would be simple to embody the convention in a couple of 
functions:

   def getslotattr(obj, slotname):
       attr = getattr(obj, slotname)
       if attr is None:
           raise AttributeError("meaningful error message")
       return attr

   def hasslotattr(obj, slotname):
       return getattr(obj, slotname, None) is not None

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list