Pre-PEP: __hashable__

Erik Max Francis max at alcyone.com
Tue Dec 10 22:58:52 EST 2002


"Delaney, Timothy" wrote:

> A new builtin function `hashable(obj)` and a new magic method
> `__hashable__(self)` are added.
> 
> `hashable(obj)` takes a single object. If the object has a
> `__hashable__()`
> method it is called, and the result returned, otherwise the object
> itself is
> returned:
> 
>     def hashable (obj):
> 
>         try:
>             return obj.__hashable__()
>         except AttributeError:
>             return self

If I understand you correctly, you want a function that will return an
immutable version of an object.  If that's the case, then the default
behavior when a __hashable__ is not defined should not be to return the
same object, but rather to raise an exception.

If the goal is to get an immutable version of an object, then if an
object gives no instructions on how to do that (i.e., the __hashable__
method is not defined), then one should assume that the object itself is
probably _mutable_, not that it is immutable.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Insight -- the titillating knack for hurting!
\__/ Colette
    The laws list / http://www.alcyone.com/max/physics/laws/
 Laws, rules, principles, effects, paradoxes, etc. in physics.



More information about the Python-list mailing list