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

Nikolaus Rath Nikolaus at rath.org
Sat May 9 06:04:26 CEST 2015


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?

Best,
-Nikolaus

-- 
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«


More information about the Python-ideas mailing list