on slices, negative indices, which are the equivalent procedures?

dn PythonList at DancesWithMice.info
Wed Aug 11 21:17:32 EDT 2021


On 12/08/2021 10.32, Dennis Lee Bieber wrote:
> On Wed, 11 Aug 2021 11:59:11 -0300, Jack Brandom <jbrandom at example.com>
> declaimed the following:
>> Where are these production rules coming from?  They're not at 
>>  https://docs.python.org/3/reference/grammar.html
>> The word ``stride'' doesn't appear in this grammar.
> 
> 	Possibly from older versions of the grammar, before simplification to
> just <expression>. Originally, the "stride" term was added in response to
> requests from the numerical library developers (NumPy seems to be the
> survivor of that cluster). At the time, it wasn't envisioned to be usable
> with regular Python objects.
> 
> https://en.wikipedia.org/wiki/Array_slicing#1991:_Python
> """
> The stride syntax (nums[1:5:2]) was introduced in the second half of the
> 1990s, as a result of requests put forward by scientific users in the
> Python "matrix-SIG" (special interest group).
> """
> 
> 	As for the grammar... Would you prefer that from 2.7?
> 
> trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
> subscriptlist: subscript (',' subscript)* [',']
> subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
> sliceop: ':' [test]
> 
> test: or_test ['if' or_test 'else' test] | lambdef
> or_test: and_test ('or' and_test)*
> and_test: not_test ('and' not_test)*
> not_test: 'not' not_test | comparison


Are you trying to give us nightmares @wulfraed?


Yes, the somewhat piecemeal coverage(s) are explained by changes over
time, and the different applications folk might apply or differing
motivations of the people involved.


I've been trying to remember if we had negative-steps in FORTRAN
do-loops especially once the capability to define subscripting-ranges
came 'in' (but can't be bothered researching further). If it was
available, or available within other, older languages, then might this
explain Python's adoption?


It is quite plain (Ref.Man 6.3.3 and 3.2) that initially the plans for
stride/step did not include the negative 'direction'. Perhaps the
initial coders thought: 'while I'm doing this, adding ... will be easy
to do', aka "famous last words", see also "YAGNI".

Negative-stepping comes-across as a bit of a party-trick. I wonder if
anyone can offer a (professional) application?

Similarly, when adding a __getitem__() to a custom-class, has anyone had
need to implement/account for negative-stepping, to achieve some purpose?


PS I'm waiting (with bated breath*) for @Chris' bared teeth...
* or is that "baited"?
-- 
Regards,
=dn


More information about the Python-list mailing list