[Python-3000] callable()

Nick Coghlan ncoghlan at gmail.com
Wed Jul 26 12:25:41 CEST 2006


Guido van Rossum wrote:
> Ouch, that's a problem indeed. There's not really a clean way out
> unfortunately: either we remove object.__hash__ and let hash() do the
> default implementation, and then presence of __hash__ is no longer an
> indicator of hashability; or we keep it, and override it to raise an
> exception in unhashable types (like list.__hash__ does); or we remove
> the hashability of objects by default, which lets us solve the above
> problems at the cost of having a convenient hash by default.
> 
> Personally, I'm not sure this problem needs solving; I don't recall
> ever needing to know whether something is hashable. So perhaps it's of
> purely theoretical importance? That would suit me fine given the above
> dilemma...

The only thing I've occasionally wanted in this area is an easy way to tell 
__getattribute__ to raise an AttributeError for attribute 'x' even if a base 
class defines it.

The use case is being able to block the inheritance of special methods that 
object provides default implementations for (like '__hash__'), such that a 
hasattr() check (or a check for a type slot being 0) for those special methods 
will actually fail.

It would be nice to be able to spell it:

   class Unhashable(object):
       undef __hash__

Although that would probably require a signature change for metaclass calling 
to accept an optional list of undefined names along with the dictionary of 
assigned names.

Alternatively, another special attribute like __slots__ could do it:

   class Unhashable(object):
       __undef__ = ["__hash__"]

Or a special value recognised by the default metaclass:

   class Unhashable(object):
       __hash__ = Undefined

When the attributes being undefined represent type slots, the corresponding 
slot would be set to 0.

Cheers,
Nick.

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


More information about the Python-3000 mailing list