When are immutable tuples *essential*? Why can't you just use lists *everywhere* instead?

Neil Cerutti horpner at yahoo.com
Mon Apr 23 11:46:13 EDT 2007


On 2007-04-23, Chris Cioffi <evenprimes at gmail.com> wrote:
> On 23 Apr 2007 17:19:15 +0200, Neil Cerutti <horpner at yahoo.com>
> wrote:
>> So the question becomes: Why do Python dictionaries require
>> keys to be of an immutable type?
>
> Dictionary keys are hashed values.  If you change the key, you
> change the hash and lose the pointer to the referenced object.

Other dictionary-like implementations (C++ std::map for example)
simply exhort you not to change keys (C++ makes it hard to ignore
the exhortation) or suffer undefined behavior. Perhaps avoiding a
cause of undefined behavior was the Python reason for the
requirement.

> Or:  Because.  ;-)

Heh, heh. I was wondering, if we dig deep enough, wether we'll
end up back at, "just an optimization," as the reason.

-- 
Neil Cerutti



More information about the Python-list mailing list