Why are tuples immutable?

Antoon Pardon apardon at forel.vub.ac.be
Fri Dec 31 16:16:50 CET 2004


On 2004-12-29, Scott David Daniels <Scott.Daniels at Acm.Org> wrote:
> Antoon Pardon wrote:
>> Op 2004-12-23, Scott David Daniels schreef <Scott.Daniels at Acm.Org>:
>>>This is half the problem.  In the period where an element is in the
>>>wrong hash bucket, a new entry for the same value can be created in
>>>the proper hash bucket.  Then the code will have to determine how to
>>>merge two entries at rehash time.
>> 
>> But similar problems can happen in a sorted list, where the sort
>> is done on a "key" of the data and where you don't want duplicate
>> "keys".
>> 
>> If you then mutate a "key", it may be possible to insert a duplicate
>> "key" in the sorted list because the list isn't sorted anymore. If
>> you then resort your list you also have to determine how to merge
>> the two items with the same "key"
> I'd say this is a stretch.  The "key" argument to sort is very new, and
> it is a function from data to a value.  The "key" can be mutated only if
> the key function is picking out a mutable part of a data element.

You could give a comparison function to the sort method since at least
version 1.4. Allowing one to sort only on part of the data or a "key".

>> This just to show that repairing sorted lists can be just as
>> troublesome as repairing dictionaries. People who think 
>> sorted lists of mutable objects is less bothersome as dictionaries
>> with mutable keys, haven't thought things through IMO.
>
> But Python has no "sorted list" data type, so it is perfectly reasonable
> to expect such lists to go through transitional representations.

Dictionaries go through transitional representations too. 
I don't see the difference here.

> A set
> should not briefly have duplicate elements, a list should not vary in 
> length when elements are replaced, an integer being continuously
> incremented should not be viewable from a separate thread as anything
> but monotonicly increasing; dictionaries are primitives and should not
> have observable transitional states.

Well a sorted list, shouldn't be temporarily unsorted. Whether that
is a primitive or not doesn't matter. Either you have some invariant
in mind that mustn't be violated or not and user invariant can be just
as important as language invariants. Are you argueing that people
should only sort immutables or should only put immutabels in a
heapqueue. If not and you see nothing wrong with people sorting
mutable objects and require the discipline of the user to not
mutate such objects, then I don't see why we simply can't expect
the same discipline from users who want mutatable objects as keys.

-- 
Antoon Pardon



More information about the Python-list mailing list