Suggestion: make sequence and map interfaces more similar

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Mar 31 08:30:29 EDT 2016


On 31/03/2016 12:58, Marco Sulla via Python-list wrote:
> On 31 March 2016 at 04:40, Steven D'Aprano <steve at pearwood.info> wrote:
>> Enough of the hypothetical arguments about what one could do or might do.
>> Let's see a concrete example of actual real world code used in production,
>> not a mickey-mouse toy program, where it is desirable that adding or
>> deleting one key will modify the rest of the keys in the mapping.
>
>
> 1. the example was for confuting your assertion that an implementation
> of sequences as extended classes of maps violate the map contract.
> 2. I already linked a real-world example previously. Google it and you
> can find tons of examples like that.
>
>
> On 31 March 2016 at 04:44, Steven D'Aprano <steve at pearwood.info> wrote:
>> for a, b in zip(spam, eggs):
>>      # do some stuff, sometimes assign x[a] or b[a] or who knows what?
>>
>>
>> Does this mean that "lists, dicts and zip" should all support the same
>> interface?
>
> I do not understand what you mean with this example. A zip object is
> not a sequence nor a map. My definition of sequences as "ordered maps
> with integer keys that start from zero and have no gaps" is perfectly
> valid as I demonstrated to you, while zip objects have nothing in
> common with sequences and maps, apart the fact they are all iterables.
>

The definition of sequence is given here 
https://docs.python.org/3/glossary.html#term-sequence.

<quote>
     An iterable which supports efficient element access using integer 
indices via the __getitem__() special method and defines a __len__() 
method that returns the length of the sequence. Some built-in sequence 
types are list, str, tuple, and bytes. Note that dict also supports 
__getitem__() and __len__(), but is considered a mapping rather than a 
sequence because the lookups use arbitrary immutable keys rather than 
integers.

     The collections.abc.Sequence abstract base class defines a much 
richer interface that goes beyond just __getitem__() and __len__(), 
adding count(), index(), __contains__(), and __reversed__(). Types that 
implement this expanded interface can be registered explicitly using 
register().
</quote>

As this is a Python list the above definition clearly takes priority 
over your definition, so can you please take this discussion offline, 
thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list