[Python-ideas] str.split() oddness

Guido van Rossum guido at python.org
Mon Feb 28 01:13:45 CET 2011


Does Ruby in general leave out empty strings from the result? What
does it return when "x,,y" is split on "," ? ["x", "", "y"] or ["x",
"y"]?

In Python the generalization is that since "xx".split(",") is ["xx"],
and "x",split(",") is ["x"], it naturally follows that "".split(",")
is [""].

On Sun, Feb 27, 2011 at 2:18 PM, Mart Sõmermaa <mrts.pydev at gmail.com> wrote:
> On Sat, Feb 26, 2011 at 11:31 PM, Arnaud Delobelle <arnodel at gmail.com> wrote:
>> On 26 February 2011 14:03, Mart Sõmermaa <mrts.pydev at gmail.com> wrote:
>>> IMHO, x.join(a).split(x) should be "idempotent"
>>> in regard to a.
>>
>> Idempotent is the wrong word here.
>
> I should have said "identity function" instead.
> Sorry for the confusion.
> (Identity function is idempotent though [1].)
>
> ~
>
> Terry, thanks for pointing out that as
>
>  string_not_containing_sep.split(sep) == [string_not_containing_sep],
>
> therefore
>
>  ''.split('b') == [''].
>
> That's the gist of it.
>
> I would like to question that reasoning though. '' (the
> empty string) is "nothing", the zero element [2] of strings.
> The problem is that it is treated as "something". I would
> say that precisely because it is the zero element,
>
>  ''.split('b')
>
> should read
>
>  "applying the split operator with any argument to the zero
>   element of strings results in the zero element of lists"
>
> and therefore
>
>  ''.split('b') == ''.split() == []
>
> (like in Ruby). And sorry for using "zero element" loosely,
> I hope it's understandable what I mean from context.
>
> ~
>
> Knowing that reasoning and the inconvenient special casing
> that it causes in actual code, would you still design split() as
> ''.split('b') == [''] today?
>
> [1] http://en.wikipedia.org/wiki/Idempotence
> [2] http://en.wikipedia.org/wiki/Zero_element
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list