How to know if an object is mutable?

Daniel Dittmar daniel.dittmar at sap.com
Tue Oct 15 04:22:49 EDT 2002


> > The subject says it all: How to know (in advance) if an
> > object is mutable?
>
> One method is to look if it is hashable. If it is hashable, it better
> be immutable, too.

That doesn't work.

>>> class Dummy:
...     pass
...
>>> d = Dummy ()
>>> d
<__main__.Dummy instance at 0x007664D0>
>>> hash (d)
7759056

And that's OK because only the part used for hash + __cmp__ has to be
immutable, the rest may change at will.

Daniel






More information about the Python-list mailing list