Modules are hashable?!

Maurice LING mauriceling at
Fri Sep 3 06:05:40 CEST 2004

Alex Martelli wrote:

> Leif K-Brooks <eurleif at> wrote:
>>I was just playing around, and noticed that modules seem to be hashable.
>>Can anyone explain that, especially given the fact that they're mutable?
> Any object x is hashable if type(x) does not expose __eq__ nor __cmp__.
> In that case, the meaning of x==y for that object is 'x is y', that is,
> id(x)==id(y), so having hash(x) return id(x) is perfectly functional.
> Mutation is not a problem if it doesn't affect equality comparisons.
> Alex

The idea that I get from reading this thread is that objects that can be 
type compared (comparing the contents) are not hashable, and they are 
list, strings, tuples and dictionary. Is there any others that fall into 
this category? Is there any way to make them hashable?

Hashable objects, on the other hand, are hashed based on say, the 
pointer address pointing the object or an identifier in the Python VM 
symbol table or something. It's like to say that when you hash a human, 
you get the name and not the physical dimensions of the person. The 
person can grow fat or slim down, but the name is still the same.


More information about the Python-list mailing list