Re: Slices by length

dn via Python-list <[email protected]>
Newsgroups gmane.comp.python.general
Organization DWM
Message-ID <[email protected]>
On 7/09/25 00: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.


0 A PEP

1 A helper-function
eg slice_by_length( input_string, start, length, )

2 Slice as an Object
run_length_slice = slice( start_index, start_index + slice_length, )
...
substring = source_string[ run_length_slice ]

3 Custom Class
with behavior similar to (1)

4 Pydantic
similar to above


Which might be best will vary by circumstance - YMMV!

-- 
Regards,
=dn

-- 
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.