[Python-ideas] The non-obvious nature of str.join (was Re: sum(...) limitation)
Terry Reedy
tjreedy at udel.edu
Mon Aug 11 21:00:26 CEST 2014
On 8/11/2014 1:14 PM, 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.)
Because s.split('') raises "ValueError: empty separator". I expected
the result should be the same as list(s), making the invariant above
true. But perhaps Guido thought splitting on '' might be a bug.
re.split('', s) returns s, which seems wrong. The doc talks about
'occurences of the pattern'. I *see* an occurance of '' at every slice
point. In a sense, Python slicing does too.
>>> 'abc'[1:1]
''
When I first learned Python, I knew to test edge case behavior rather
than depend on my interpretation of docs, or worse, my expectations from
previous experience and knowledge. The interactive interpreter makes
little tests like 'ab'split('') and re.split('', 'ab') trivial and
faster than reading (or debugging).
--
Terry Jan Reedy
More information about the Python-ideas
mailing list