<div dir="auto"><span style="font-family:sans-serif">+1 Good performance analysis IMHO :)</span></div><br><div class="gmail_quote"><div dir="ltr">On Tue, 29 Jan 2019, 22:24 Jonathan Fine <<a href="mailto:jfine2358@gmail.com">jfine2358@gmail.com</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I've not been following closely, so please forgive me if I'm repeating<br>
something already said in this thread.<br>
<br>
Summary: str.join allows us to easily avoid, when assembling strings,<br>
1. Quadratic running time.<br>
2. Redundant trailing comma syntax error.<br>
<br>
The inbuilt help(str.join) gives:<br>
    S.join(iterable) -> str<br>
        Return a string which is the concatenation of the strings in the<br>
        iterable.  The separator between elements is S.<br>
<br>
This is different from sum in two ways. The first is the separator S.<br>
The second is performance related. Consider<br>
    s = 0<br>
    for i in range(100):<br>
        s += 1<br>
and<br>
    s = ''<br>
    for i in range(100):<br>
        s += 'a'<br>
<br>
The first has linear running time (in the parameter represented by<br>
100). The second has quadratic running time (unless string addition is<br>
doing something clever, like being lazy in evaluation).<br>
<br>
The separator S is important. In Python a redundant trailing comma, like so,<br>
    val = [0, 1, 2, 3,]<br>
is both allowed and useful. (For example, when the entries are each on<br>
a simple line, it reduces the noise that arises when an entry is added<br>
at the end. And when the entries are reordered.)<br>
<br>
For some languages, the redundant trailing comma is a syntax error. To<br>
serialise data for such languages, you can do this:<br>
    >>> '[{}]'.format(', '.join(map(str, v)))<br>
    '[0, 1, 2, 3]'<br>
<br>
>From here, by all means repackage for your own convenience in your own<br>
library, or use a third party library that already has what you want.<br>
(A widely used pypi package has, I think, a head start for adoption<br>
into the standard library.)<br>
<br>
By the way, as search for "python strtools" gives me<br>
<a href="https://pypi.org/project/extratools/" rel="noreferrer noreferrer" target="_blank">https://pypi.org/project/extratools/</a><br>
<a href="https://www.chuancong.site/extratools/functions/strtools/" rel="noreferrer noreferrer" target="_blank">https://www.chuancong.site/extratools/functions/strtools/</a><br>
<br>
<a href="https://pypi.org/project/str-tools/" rel="noreferrer noreferrer" target="_blank">https://pypi.org/project/str-tools/</a>. # This seems to be an empty stub.<br>
<br>
-- <br>
Jonathan<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank" rel="noreferrer">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>