[Python-ideas] The non-obvious nature of str.join (was Re: sum(...) limitation)

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Aug 11 19:14:25 CEST 2014


On Mon, Aug 11, 2014 at 12:47 PM, Stephen Hansen <me+python at ixokai.io>
wrote:

> Yes, sep.join(list) is a bit of a weird construct, but its one thing to
> learn, and its not a hard one to teach at that. In fact, it makes very
> logical sense once you explain it and makes people think of things more
> Pythonically after. I say from experience, not in theory. But,
> string.split(sep) is very natural. You seem to think that they need to be
> in the same order to be obvious but I don't see why nor do I think any of
> the alternatives are not without problems that are bigger issues.


I am not suggesting any changes to str.join or str.split methods.  I am
just arguing that sum(list_of_strings, '') should be allowed by the
language and its performance is a matter of the quality of implementation.
 Once you learn that string addition is concatenation in Python, it is
natural to "sum" lists of Python strings regardless of whether  it makes
sense in your native language.

sep.join(list) is not such a weird construct when sep is non-empty - it is
the sep='' case which is weird and non-obvious.  (Note that someone in this
thread suggested demonstrating s == sep.join(s.split(sep)) invariant as a
teaching tool, but this invariant fails when sep is empty.)  When you are
tasked with finding s1 + s2 + ... + sN given [s1, s2, ..., sN], it is sum
that first comes to mind, not join.  The situation is different when you
have a separator to begin with, but when you don't using an empty separator
feels like a performance hack in the absence of an efficient natural
solution.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140811/c59c33b6/attachment-0001.html>


More information about the Python-ideas mailing list