Consider:

>>> ''.split()
[]

That seems okay... split an empty string on whitespace and you get an empty list, sure, why not.

Except that in every other case, if you split a string on a separator that it doesn't contain, you get a list containing the original string - even if it's an empty string.

So, these are all consistent:

>>> 'no'.split()
['no']
>>> 'no'.split('yes')
['no']
>>> ''.split('yes')
['']
>>> 'no'.split(' ')
['no']
>>> ''.split(' ')
['']
>>> ''.split('\t')
['']

And if those all look reasonable, then this one that we started with suddenly looks very wrong by comparison:

>>> ''.split()
[]

""" ... Special cases aren't special enough to break the rules. Although practicality beats purity. ... """

Is this case really practical? I've been tripped up by forgetting that ''.split() is radically different than ''.split(' '), and I've seen other people make the same mistake.


Comments

comments powered by Disqus