Modules are hashable?!
jmlai at uci.edu
Wed Sep 1 12:51:53 CEST 2004
Leif K-Brooks 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?
Most objects in Python are hashable, and also mutable. If you define a
class Foo and create x = Foo(), x will be hashable. But you can also do
x.stuff = 3. So modules appear to be pretty much regular objects, which
are usually hashed by ID -- which will be unique for any two objects.
Try hash(sys) == id(sys) to see for yourself.
Lists, tuples, dicts, strings, and a few other things are odd in that
they're hashed/compared by their contents rather than ID. So you can
have two different objects that are equivalent with regards to hashing
Due to the way hashing works, the hash value is not allowed to change
while it's in a dict. IDs never change, so regular objects have an
unchanging hash value even if you change their contents. For the special
objects, changing the contents would change the hash value, and that's
I think that's how it works, anyway :P
- Jason Lai
More information about the Python-list