Two python issues
Cameron Simpson
cs at cskk.id.au
Tue Nov 5 16:06:12 EST 2024
On 05Nov2024 15:48, Raymond Boute <raymond.boute at pandora.be> wrote:
>Python seem to suffer from a few poor design decisions regarding
>strings and lists that affect the elegance of the language.
>
>(a) An error-prone "feature" is returning -1 if a substring is not
>found by "find", since -1 currently refers to the last item.
`find` is a pretty old API interface. It is what it is. It may obtain
some of its design choices from C style calls where returning -1 for
failure was a common idiom.
>If "find" is unsuccessful, an error message is the only clean option.
This is not true. Often we want to look for something, and act one way
or another depending on whether it is found. I've got plenty of loops
and other tests which more or less go "run until this is not found". It
is not an error, it is just a circumstance to accomodate.
>Moreover, using index -1 for the last item is a bad choice: it should
>be len(s) - 1 (no laziness!).
>Negative indices should be reserved for elements preceding the element
>with index 0 (currently not implemented, but a must for orthogonal
>design supporting general sequences).
It is _far_ too late to propose such a change.
Plenty of us are quite hapoy with negative indices. We just view them as
counting backwarss from the end of the string or sequence instead of
forwards from the beginning.
>(b) When using assignment for slices, only lists with the same length
>as the slice should be acceptable, otherwise an error should be
>given.
There are many many circumstances where we replace a subsequence with a
different subsequence of different length. Outlawing such a thing would
remove and extremely useful feature.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list