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

Terry Reedy tjreedy at udel.edu
Mon Aug 11 21:55:35 CEST 2014


On 8/11/2014 9:56 AM, Alexander Belopolsky wrote:
>
> On Mon, Aug 11, 2014 at 2:56 AM, Wolfgang Maier
> <wolfgang.maier at biologie.uni-freiburg.de
> <mailto:wolfgang.maier at biologie.uni-freiburg.de>>
> wrote:
>
>     I am using Python for teaching programming to absolute beginners at
>     university and, in my experience, joiner.join is never a big hurdle.
>
>
> In my experience, it is the asymmetry between x.join(y) and x.split(y)
> which causes most of the confusion.   In x.join(y), x is the separator

Given that the two parameters of join are a concrete string and the 
abstraction 'iterable of strings', join can only be a method of the joiner.

I would first teach ' '.join(it_of_strings) as the 'normal' case of 
joining 'words', along with print with the default sep = ' '.


> and y is the data being joined, but in x.split(y), it is the other way
> around.

*If* sep is present, then sep.split(string) would be possible. But when 
sep is *not* not present, split cannot be a method of something that is 
not there.  So I think I would teach s.split() first and then add 
.split(sep) and .splitlines().

I would also teach join and split together since they are, at their 
cores (excluding special cases), inverses.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list