<div dir="ltr">This was a CPython idea after all, so I was assuming a C implementation, which means that new flags would have to be added to the string object to denote a string slice, etc.<div><br></div><div>Like I said in another message: it's not that important to me though.  I was just curious as to why CPython was designed so that string slicing is linear rather than constant time given that strings are constant.</div><div><br></div><div>I'm getting the impression that the payoff is not worth the complexity.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 7, 2015 at 4:12 PM, Skip Montanaro <span dir="ltr"><<a href="mailto:skip.montanaro@gmail.com" target="_blank">skip.montanaro@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra">I haven't seen anyone else mention it, so I will point out: interoperability with C. In C, strings are NUL-terminated. PyStringObject instances do (or used to) have NUL-terminated strings in them. According to unicodeobject.h, that seems still to be the case:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra"><font face="monospace, monospace">typedef struct {</font></div><div class="gmail_extra"><font face="monospace, monospace">    /* There are 4 forms of Unicode strings:</font></div><div><font face="monospace, monospace">    ...</font></div></div><div class="gmail_extra"><font face="monospace, monospace">    wchar_t *wstr;              /* wchar_t representation (<b>null-terminated</b>) */</font></div><div class="gmail_extra"><div class="gmail_extra"><font face="monospace, monospace">} PyASCIIObject;</font></div><div><br></div></div><div class="gmail_extra">and:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra"><font face="monospace, monospace">typedef struct {</font></div><div class="gmail_extra"><font face="monospace, monospace">    PyASCIIObject _base;</font></div><div class="gmail_extra"><font face="monospace, monospace">    Py_ssize_t utf8_length;     /* Number of bytes in utf8, <b>excluding the</b></font></div><div class="gmail_extra"><font face="monospace, monospace"><b>                                 * terminating \0</b>. */</font></div><div class="gmail_extra"><font face="monospace, monospace">    char *utf8;                 /* UTF-8 representation (<b>null-terminated</b>) */</font></div><div class="gmail_extra"><font face="monospace, monospace">    Py_ssize_t wstr_length;     /* Number of code points in wstr, possible</font></div><div class="gmail_extra"><font face="monospace, monospace">                                 * surrogates count as two code points. */</font></div><div class="gmail_extra"><font face="monospace, monospace">} PyCompactUnicodeObject;</font></div><div><br></div></div><div class="gmail_extra">The raw string is NUL-terminated, precisely so copying isn't required in most cases before passing to C. Making s[1:-1] a view onto the underlying string data in s would require you to copy the data when you want to pass the view into C so you could tack on that NUL. That happens a lot, so it's likely you wouldn't save much work, and result in a lot more churn in Python's memory allocator. The only place you could avoid the copy is if the view you are dealing with is a strict suffix of s.</div><span class="HOEnZb"><font color="#888888"><div class="gmail_extra"><br></div><div class="gmail_extra">Skip</div><div class="gmail_extra"><br></div></font></span></div><div class="HOEnZb"><div class="h5">

<p></p>

-- <br>
<br>
--- <br>
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.<br>
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/python-ideas/II-4QRDb8Is/unsubscribe" target="_blank">https://groups.google.com/d/topic/python-ideas/II-4QRDb8Is/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href="mailto:python-ideas+unsubscribe@googlegroups.com" target="_blank">python-ideas+unsubscribe@googlegroups.com</a>.<br>
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank">https://groups.google.com/d/optout</a>.<br>
</div></div><br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
<br>
--<br>
<br>
---<br>
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.<br>
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/python-ideas/II-4QRDb8Is/unsubscribe" target="_blank">https://groups.google.com/d/topic/python-ideas/II-4QRDb8Is/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href="mailto:python-ideas%2Bunsubscribe@googlegroups.com">python-ideas+unsubscribe@googlegroups.com</a>.<br>
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank">https://groups.google.com/d/optout</a>.<br>
<br></blockquote></div><br></div>