[Python-ideas] This seems like a wart to me...

Guido van Rossum guido at python.org
Thu Dec 11 16:51:38 CET 2008


On Thu, Dec 11, 2008 at 6:18 AM,  <skip at pobox.com> wrote:
> Python 2 and 3 both exhibit this behavior:
>
>    >>> "".split()
>    []
>    >>> "".split("*")
>    ['']
>    >>> "".split(" ")
>    ['']
>
> It's not at all clear to me why splitting an empty string on implicit
> whitespace should yield an empty list but splitting it with a non-whitespace
> character or explicit whitespace should yield a list with an empty string as
> its lone element.  I realize this is documented behavior, but I can't for
> the life of me understand what the rationale might be for the different
> behaviors.  Seems like a wart which might best be removed sometime in 3.x.

Which of the two would you choose for all? The empty string is the
only reasonable behavior for split-with-argument, it is the logical
consequence of how it behaves when the string is not empty. E.g.
"x:y".split(":") -> ["x", "y"], "x::y".split(":") -> ["x", "", "y"],
":".split(":") -> ["", ""]. OTOH split-on-whitespace doesn't behave
this way; it extracts the non-empty non-whitespace-containing
substrings.

If anything it's wrong, it's that they share the same name. This
wasn't always the case. Do you really want to go back to .split() and
.splitfields(sep)?

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-ideas mailing list