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

Roy Smith roy at panix.com
Sun Feb 10 17:44:12 EST 2002


Erik Max Francis <max at alcyone.com> wrote:
> That is simply not comparable.  The purpose of split is to divide up a
> string according to a delimiter.  Asking for it to split a string given
> a null delimiter doesn't make much sense at all;

I think it makes plenty of sense.

> if you're calling
> string.split with the intent of dividing it up and you're passing in ''
> as a delimiter, that's almost certainly a mistake.

If the delimter was written as a constant '', I might be inclined to agree, 
but it could be computed on the fly, and having a delimter of '' mean 
"split the thing up with nothing between the parts" is a very logical 
extension of the general idea.

Should we outlaw raising things to the zero power because somebody thinks 
it "doesn't make much sense at all"?  I'll be a lot of people think it 
doesn't make much sense, but the mathematicians would howl if you tried to 
take it away.

In everyday usage, raising something to the zero power, I'll agree, doesn't 
make a lot of sense.  But, the math folks figured out a reasonable, 
consistant, and logical meaning for it, by extrapolating from other 
exponentiation and seeing what would fit into a logical whole.  The same 
thing should be done with split().  Having split('') mean "split every 
character" is the logical extension of its other uses.

None of the following are illegal..

for i in []:
   print "can't get here"

if ('a' in ''):
   print "can't get here either"

while (0):
   print "what do you think?"

class theNullClass:
   pass

Not to mention a zillion variations on identity operations like adding zero 
or multiplying by one.

You could make rational arguments that none of them "make much sense at 
all", and that they are all likely to be errors, but you'd get arguments 
that to outlaw them would destroy the orthogonality of the language.

But I think the strongest argument in favor of allowing '' as an argument 
to split() is the join() symmetry argument.



More information about the Python-list mailing list