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

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Mon Aug 11 21:38:51 CEST 2014


On 11.08.2014 19:14, Alexander Belopolsky wrote:
>
> 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.)

For the record, this doesn't fail because of any weirdness about 
''.join(s). It's just that s.split() does not take an empty string as 
separator. So, ok, I should have said:

s == sep.join(s.split(sep)) for any allowed sep (which should be an 
obvious requirement)

but this has nothing to do with the rest of the discussion other that it 
is a bit peculiar that join and sep do not act perfectly symmetrical.
On the other hand, a builtin function sum and a string method split 
would be alot more asymmetric.

> When you are tasked with finding s1 + s2 + ... + sN given [s1,
> s2, ..., sN], it is sum that first comes to mind, not join.

Not *my* first thought when it comes to strings, but if it is yours, 
then you try it once and you get an appropriate error message pointing 
you to the correct solution. Ok for me.

>  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.
>




More information about the Python-ideas mailing list