[Python-ideas] Why don't CPython strings implement slicing using a view?
Chris Angelico
rosuav at gmail.com
Sat May 9 07:06:07 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?
Oops, mis-sent (stupid touchpad on this new laptop). Trying again.
There might be multiple views, so a hard-coded refcount-of-one check
wouldn't work. The view would need to keep a weak reference to its
underlying string - but not in the sense of the Python weakref module,
which doesn't seem to have any notion of "about to be garbage
collected", but only "has now been garbage collected". Notably, by the
time a callback gets called, it's too late to retrieve information
from the callback itself. A modified form of weakref could do it,
though; with the understanding that the referents are immutable, and
premature transform from view to coalesced slice has no consequence
beyond performance, this could be done.
Ideally, it'd be an entirely invisible optimization.
ChrisA
More information about the Python-ideas
mailing list