Why are tuples immutable?
adam at cognitcorp.com
Thu Dec 16 05:38:04 CET 2004
On Wed, 2004-12-15 at 10:26, Jp Calderone wrote:
> On Wed, 15 Dec 2004 14:18:21 GMT, Roel Schroeven <rschroev_nospam_ml at fastmail.fm> wrote:
> >Antoon Pardon wrote:
> > > Op 2004-12-15, Fredrik Lundh schreef <fredrik at pythonware.com>:
> > >>sorry, but I don't understand your reply at all. are you saying that dictionaries
> > >>could support mutable keys (e.g lists) by making a copy of the key? how would
> > >>such a dictionary pick up changes to the original key object? (I'm talking about
> > >>the key stored in the dictionary, not the key you're using to look things up).
> > >
> > >
> > > You want to mutate a key that is within a dictionary?
> > No, we don't want to mutate it; as far as I know, that is exactly the
> > reason why dictionaries don't support mutable keys.
> Dictionaries support mutable keys just find. What they don't
> support is unhashable keys.
> For some objects, this is an important distinction: lists are
> mutable but not hashable.
> For other objects, it is not: instances of user defined classes
> are mutable and hashable. This is handy since the default hash is
> based on identity instead of the values of attributes.
> Mutating an object in a dictionary is completely reasonable. What
> is unreasonable is wanting to make a change that would change its
> hash value.
And how exactly do you propose to mutate an object without changing its
* Create this mutate-able object type.
* Create two objects that are different with different hash values.
* Mutate one so that it is the same as the other.
* Compare their hash values.
If the hash values are the same, you lose because you didn't keep your
If the hash values are different then you just broke your object because
two objects with the same value must have the same hash value, but yours
A hash value is simply a probabilistic mechanism to quickly determine if
two objects must be different, or to select a bin in a hash table.
Two different objects are allowed to have the same hash value, but two
objects that are the same must have the same hash value. And saying
that all of your objects have the same hash value to accommodate a
dictionary isn't doing the dictionary a favor because you would have
just turned it into a linked list by doing so ...
More information about the Python-list