On Tue, Jan 29, 2019 at 09:21:48PM +0000, Jonathan Fine wrote: > I've not been following closely, so please forgive me if I'm repeating > something already said in this thread. > > Summary: str.join allows us to easily avoid, when assembling strings, > 1. Quadratic running time. > 2. Redundant trailing comma syntax error.
The lack of a syntax error for trailing commas is a language-wide feature that has nothing to do with str.join. > The inbuilt help(str.join) gives: > S.join(iterable) -> str > Return a string which is the concatenation of the strings in the > iterable. The separator between elements is S. > > This is different from sum in two ways. Three ways. sum() intentionally doesn't support strings at all: py> sum(['a', 'b', 'c'], '') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sum() can't sum strings [use ''.join(seq) instead] unless you cleverly defeat this intentional limitation. (How to do this is left as an exercise for the reader.) > The first is the separator S. > The second is performance related. Consider > s = 0 > for i in range(100): > s += 1 > and > s = '' > for i in range(100): > s += 'a' > > The first has linear running time (in the parameter represented by > 100). The second has quadratic running time (unless string addition is > doing something clever, like being lazy in evaluation). In CPython, string addition does often do something clever. But not by being lazy -- it optimizes the string concatenation by appending to the strings in place if and only if it is safe to do so. -- Steve _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/