tuples, index method, Python's design

Paul Boddie paul at boddie.org.uk
Thu Apr 12 06:06:02 EDT 2007


On 12 Apr, 09:37, Antoon Pardon <apar... at forel.vub.ac.be> wrote:
>
> Why then does python itself provide immutables? I find this reasoning
> more than baffling. There has been all these arguments about why
> it is best to use immutables as dictionary keys.

You've answered your own question. If you had a mutable dictionary
key, stored something in a dictionary using that key, then modified
the key and tried to retrieve the stored item using that same key
object, you might never find that item again. This is something of a
simplification (you'd have to look into details of things like
__hash__ and __eq__, I imagine), but this is just one area where
immutability is central to the operation of the feature concerned.

Other languages provide some control over immutability with things
like "const", and there are good reasons for having such things,
although you do need to know what you're doing as a programmer when
using them. Some people might argue that the dictionary key example
given above is contrived: "Of course it won't work if you modify the
key!" they might say. Having some idea of which objects are immutable
can provide some protection from inadvertent mutation, however.

> But the moment the topic changes, someone else comes with the comment that
> wanting your sequence to be immuatble is unpythonic.

As soon as "unpythonic" is mentioned we enter subjective territory.

Paul




More information about the Python-list mailing list