[Python-ideas] Why don't CPython strings implement slicing using a view?
Mark Lawrence
breamoreboy at yahoo.co.uk
Thu May 7 22:02:19 CEST 2015
On 07/05/2015 20:29, Neil Girdhar wrote:
> The point is to have a Pythonic way of saying that. Using islice or
> iterating over a range and indexing is ugly. It would be cleaner to
> implement a string class that implements fast slicing than those unpythonic
> pieces of code.
>
> Best,
>
> Neil
I don't see anything unpythonic there at all, just standard Python. If
you want fast slicing that badly you can write the class unless somebody
beats you to it as their itch is more painful.
>
> On Thu, May 7, 2015 at 2:26 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>
>> On 5/7/2015 11:46 AM, Steven D'Aprano wrote:
>>
>>> On Wed, May 06, 2015 at 07:05:15PM -0700, Neil Girdhar wrote:
>>>
>>>> Since strings are constant, wouldn't it be much faster to implement
>>>> string
>>>> slices as a view of other strings?
>>>>
>>>
>>> String or list views would be *very* useful in situations like this:
>>>
>>> # Create a massive string
>>> s = "some string"*1000000
>>> for c in s[1:]:
>>> process(c)
>>>
>>
>> Easily done without slicing, as discussed on python-list multiple times.
>>
>> it = iter(s)
>> next(it)
>> for c in it: process(c)
>>
>> for s[5555: 399999], use explicit indexes
>>
>> for i in range(5555, 400000): process s[i]
>>
>> or use islice.
>>
>> The use case for sequence views is when one needs to keep around both the
>> base sequence and the slices (views).
>>
>> --
>> Terry Jan Reedy
>>
--
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-ideas
mailing list