Suggestion: make sequence and map interfaces more similar

Steven D'Aprano steve at pearwood.info
Sun Mar 27 21:05:46 EDT 2016


On Mon, 28 Mar 2016 05:01 am, Marco S. wrote:

> Steven D'Aprano wrote:
> 
>> The point you might have missed is that treating lists as if they were
>> mappings violates at least one critical property of mappings: that the
>> relationship between keys and values are stable.
> 
> 
> This is true for immutable maps, but for mutable ones, you can simply do

No, it is true for mutable maps too.

When you add a new key:value to a dict, the other key:value pairs don't
change. That is the whole point of a mapping! Of course you can
deliberately change the value by re-assignment:

    map[key] = new_value

but that's not what I'm talking about. When you add a NEW key, the OTHER
keys DON'T change. That is ABSOLUTELY CRITICAL to a mapping. Anything which
lacks that property is not a mapping.

The relationship between the index of a value and the value in a sequence is
not stable. Inserting a new value can change the "key"(actually index) of
some or all of the existing values. The whole point of sequences is that
the position of values is NOT stable: they are intended to move around. You
can sort them, reverse them, delete them, insert new values, and the others
will move around to make room. If you think of the index as a key, this is
completely the opposite behaviour of mappings.

In a mapping, the key:value is stable, and must not change unless you
explicitly change it.

In a sequence, the index:value relationship is unstable, and can easily
change without notice, due to many different operations:

insertion
deletion
popping a value
sorting
shuffling
reversal of the sequence

and probably more.



-- 
Steven




More information about the Python-list mailing list