Why are tuples immutable?
apardon at forel.vub.ac.be
Wed Dec 22 09:12:29 CET 2004
Op 2004-12-21, Fredrik Lundh schreef <fredrik at pythonware.com>:
> Jeff Shannon wrote:
>> So show us a dictionary (i.e. hash table) implementation that can do this. You'll need to be able
>> to derive the old hash from the new hash, of course, so that you can correctly associate the
>> values already stored under that key. And you'll have to be able to do that without referring to
>> the pre-modified object again, because dicts can't track every Python operation that might modify
>> a key object. And it needs to deal properly with equivalent-but-different-ID list literals,
>> because using literals (and not references stored elsewhere) is an important and common dict key
> and to temporarily refer back to the top of this thread, do all this without
> any performance impact, compared to the current implementation.
Why should that be? This originated when someone argued that lists could
easily be resorted and reheapified. But resorting en reheapifying is has
an extra cost to compared to keeping your list sorted or as a heap by
not allowing the elements to be mutated.
So why should violating and repairing invariants be allowed to have a
performance inpact, except when it is done with dictioary keys.
More information about the Python-list