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

Neil Girdhar mistersheik at gmail.com
Sat May 9 10:53:47 CEST 2015


Why not?  You can compare numpy array views, can't you?

In [4]: a = np.array([1,2])

In [5]: a[1:] < a[:1]
Out[5]: array([False], dtype=bool)

On Sat, May 9, 2015 at 4:51 AM, 'Mark Lawrence' via python-ideas <
python-ideas at googlegroups.com> wrote:

> On 09/05/2015 08:31, Andrew Barnert via Python-ideas wrote:
>
>> On May 8, 2015, at 21:04, 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?
>>>
>>
>> Of course I could be wrong; we won't really know until someone actually
>> builds at least an implementation and tests it.
>>
>
> Well they can, but I found a major problem with views is that you can't
> compare them and so can't sort them, thus rendering them useless for a lot
> of applications.
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> _______________________________________________
> 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/20150509/9cf34e53/attachment.html>


More information about the Python-ideas mailing list