why are some types immutable?

Roy Smith roy at panix.com
Sun Jan 16 12:41:02 EST 2005


In article <1105896839.966379.220890 at z14g2000cwz.googlegroups.com>,
 "Dan Bishop" <danb_83 at yahoo.com> wrote:

> Roy Smith wrote:
> > Torsten Mohr <tmohr at s.netic.de> wrote:
> > > reading the documentation (and also from a hint from this NG)
> > > i know now that there are some types that are not mutable.
> > >
> > > But why is it this way?
> > >
> > > From an overhead point of view i think it is not optimal,
> > > for example for a large string it could be much faster to
> > > have it changed in place, not generating a new one for
> > > every step in a change.
> >
> > There has been a huge amount written about immutable types recently.
> A
> > search of the Google news archives should turn up tons of articles.
> >
> > But, in a nutshell, the biggest reason for immutable types (tuples
> and
> > strings) is that this lets they be dictionary keys.  If you want to
> know
> > why dictionary keys have to be immutable,
> 
> More precisely, dictionary keys can't be mutable in any way that
> affects the result of the hash function or the == or != operators.

Yes, this is true.  In fact, I spent a fair amount of time in the 
referenced thread arguing exactly that point.  I was hoping people would 
take my hint, read the thread, and find that out :-)



More information about the Python-list mailing list