Bug or feature? 'abc'.split('') rejects empty separator

Bengt Richter bokr at oz.net
Mon Feb 11 00:33:55 EST 2002


On Sun, 10 Feb 2002 18:05:01 -0500, "Tim Peters" <tim.one at home.com> wrote:

>[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'
Of course you mean
 >>> ''.join(['abc'])
 'abc'

This option is also compatible with what re does:
 >>> import re
 >>> re.split('','abc')
 ['abc']
 
(Note absence of complaint ;-)

>
>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.
>
Which is what I was trying to get at with:
--
or, ugly, but consistent with *find results
(finding the separator front, back, and between all):

 >>> ';a;b;c;'.split(';')
 ['', 'a', 'b', 'c', '']
--
I.e., return the latter value, as you say.
Which also joins ok-ly:

 >>> ''.join(['', 'a', 'b', 'c', ''])
 'abc'


OTOH:

 [21:33] C:\pywk>perl "-e print join('-', split(//, 'abc'))"
 a-b-c

Not sure which way that'll throw things ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list