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 <> 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:]:

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?

Python-ideas mailing list
Code of Conduct:


You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit
To unsubscribe from this group and all its topics, send an email to
For more options, visit