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

Ed Kellett edk141 at gmail.com
Thu May 7 21:57:08 CEST 2015


On Thu, 7 May 2015 at 20:40 Andrew Barnert via Python-ideas <
python-ideas at python.org> wrote:
>
> One question: when you slice a view, do you get a copy, or another view?
> Because if it's the latter, you can write view slices with view(s)[1:]
> instead of view(s, 1, None), which seems like a big readability win, but on
> the other hand it means a view doesn't act just like a normal
> sequence--e.g., v[:] no longer makes a copy. (NumPy does the latter, of
> course.)


Well, in the context of strings it doesn't matter. (or, in some sense, not
copying immutable strings is a viable implementation technique for copying
them). CPython already knows that:

>>> x = "foo"
>>> x is x[:]
True

Ed Kellett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150507/d34d911f/attachment-0001.html>


More information about the Python-ideas mailing list