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

Neil Girdhar mistersheik at gmail.com
Thu May 7 18:22:40 CEST 2015


One way, is leave CPython as is, and create a string view class as a user.
The other is to make the views use weakref to the target string and copy on
delete.

Anyway, I'm not really bothered about this.  Just wanted to see what people
thought.  You probably shouldn't be using str for really long strings,
which is the only time this would matter anyway.

On Thu, May 7, 2015 at 11:46 AM, Steven D'Aprano <steve at pearwood.info>
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)
>
>
> which needlessly duplicates almost the entire string just to skip the
> first char. The same applies to lists or other sequences.
>
> 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?
>
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/II-4QRDb8Is/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150507/8941b936/attachment.html>


More information about the Python-ideas mailing list