Re: Slices by length

Rob Cliffe via Python-list <[email protected]>
Newsgroups gmane.comp.python.general
Message-ID <[email protected]>

On 07/10/2025 20:37, Thomas Passin wrote:
> On 10/7/2025 2:49 PM, Rob Cliffe via Python-list wrote:
>>
>>
>> On 06/09/2025 17:21, MRAB wrote:
>>> On 2025-09-06 13:47, Rob Cliffe via Python-list wrote:
>>>> I quite often find myself writing expressions of the form
>>>>       someString[x : x+n]
>>>> where n is often an int and x may be an int, a variable, or a 
>>>> (possibly
>>>> complicated) expression.
>>>> It would be more natural to be able to specify the slice not by its
>>>> startpoint and ENDPOINT,
>>>> but by its startpoint and LENGTH, particularly when x is a long
>>>> expression and has to be repeated or pre-stored in a variable.
>>>> I have worked with languages that had this facility, and I miss it.
>>>> The possible syntaxes are restricted by the need to avoid ambiguity 
>>>> with
>>>> existing valid code.
>>>> It could be for example
>>>>       someString[x ! n]
>>>> (of course, x and n could be arbitrary expressions).
>>>> One point to be decided would be whether negative n would produce
>>>>       (a) an empty string, so that s[x ! n] was always the same as 
>>>> s[x :
>>>> x+n]
>>>>       (b) a slice ending at x, e.g. "01234567"[4!-3] would equal "123"
>>>> (or less plausibly "321").
>>>> I don't have a strong opinion on this; there may be good reasons for
>>>> preferring one to another.
>>>> Does anybody think this is a good idea?
>>>>
>>> I'm not happy about the syntax.
>>> How about `someString[x +: n]`?
>> Yes thank you, I think that's a great improvement, and of course it's 
>> currently not legal syntax.
>> Best wishes
>> Rob Cliffe
>>
>>>
>>> As for a negaitve length, it should probably return an empty string 
>>> given that `someString[x : x+n]` returns an empty string if `n` is 
>>> negative.
>>>
>>> The syntax would leave open the possibility of adding `someString[x 
>>> -: n]` in the future for option b.
>>>
>>> On a related note, a quick search revealed that the D programming 
>>> language lets you write `someString[x .. $]` where `$` in that 
>>> context is the length. That lets you write `someString[x .. $ - n]`. 
>>> In Python that could be `someString[x : $ - n]`, equivalent to 
>>> `someString[x : len(someString) - n]`, even if `n` is 0.
>
> Since there are at least two options, (a) and (b), the proposed syntax 
> is not obvious and intuitive. So it would add to the programmer's 
> cognitive load for no or little benefit.  Let's not advocate for 
> adding to cognitive load.
>
Thanks for your answer, but I don't really agree.
It may not be obvious now that we're all used to it, but there are also 
two possible meanings for "abcd"[1:-1].  Should it be "bc" (as it is), 
or an empty string?
As for cognitive load:
     (a) Someone may think of an even better syntax which makes it more 
intiuitive.
           Maybe s[x->n]   ?  I'd welcome more suggestions.
     (b) When the slice start is a conplicated expression and the slice 
length is very simple (like a literal int), this proposal would remove 
the need to either repeat the expression or first put it in a temporary 
variable, which may well improve readability.

In fairness, it is already possible to write
      s[x:][:n]
which gives the "right" answer if n is non-negative, and is quite readable,
although this is slower than s[x:x+2], at least in tests that I did 
(and, I would hazard a guess, always).
Best wishes
Rob Cliffe
-- 
https://mail.python.org/mailman3//lists/python-list.python.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.