[Python-ideas] Why don't CPython strings implement slicing using a view?

Chris Angelico rosuav at gmail.com
Sat May 9 07:01:42 CEST 2015


On Sat, May 9, 2015 at 2:04 PM, Nikolaus Rath <Nikolaus at rath.org> wrote:
> On May 07 2015, Steven D'Aprano <steve-iDnA/YwAAsAk+I/owrrOrA at public.gmane.org> wrote:
>> But a view would be harmful in this situation:
>>
>> s = "some string"*1000000
>> t = s[1:2]  # a view maskerading as a new string
>> del s
>>
>> Now we keep the entire string alive long after it is needed.
>>
>> How would you solve the first problem without introducing the second?
>
> Keep track of the reference count of the underlying string, and if it
> goes down to one, turn the view into a copy and remove the sliced
> original?
>

T


More information about the Python-ideas mailing list