[Python-ideas] Why don't CPython strings implement slicing using a view?
Steven D'Aprano
steve at pearwood.info
Thu May 7 17:46:21 CEST 2015
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
More information about the Python-ideas
mailing list