Bug or feature? 'abc'.split('') rejects empty separator
Tim Peters
tim.one at home.com
Sun Feb 10 18:05:01 EST 2002
[Bengt Richter]
> >>> 'abc'.split('')
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: empty separator
>
> Wouldn't it make sense to return list('abc') ?
[Neil Schemenauer]
> It would also make sense to return ['a', 'b', 'c'].
Well, that's what list('abc') does return, so you're in violent agreement.
However, this still stands:
> Since it's not obvious what you want Python raises an error.
Indeed, my first thought was "OK, if it has to return *something*, then
since we're asking it to split on nothing, it shouldn't split at all:
['abc']
is what it should return." That's also compatible with
''.join[['abc']] == 'abc'
People who think '' should split "everywhere" instead of "nowhere" should
really be arguing for 'abc'.split('') to return
['', 'a', 'b', 'c', '']
instead, since in any sense that '' could be said to "match", it matches at
4 slice positions in 'abc', not 2.
More information about the Python-list
mailing list