[Python-ideas] str.split() oddness

Mart Sõmermaa mrts.pydev at gmail.com
Sun Mar 6 22:06:08 CET 2011


On Sun, Mar 6, 2011 at 9:35 PM, Georg Brandl <g.brandl at gmx.net> wrote:
> On 06.03.2011 19:32, Mart Sõmermaa wrote:
>
>>> In Python the generalization is that since "xx".split(",") is ["xx"],
>>> and "x",split(",") is ["x"], it naturally follows that "".split(",")
>>> is [""].
>>
>> That is one line of reasoning that emphasizes the
>> "string-nature" of ''.
>>
>> However, I myself, the Ruby folks and Nick would rather
>> emphasize the "zero-element-nature" [1] of ''.
>>
>> Both approaches are based on solid reasoning, the latter
>> just happens to be more practical.
>
> I think we haven't seen any proof of that (and no, the property
> of x.join(a).split(x) == a is not show me why it would be practical).

I referred to the practical example in my first message,
but let me repeat it.

Which do you prefer:

  bar = dict(chunk.split('=') for chunk in foo.split(","))

or

  bar = (dict(chunk.split('=') for chunk in foo.split(",")) if foo else {})

?

I'm afraid there are other people besides me that fail to think
of the `if foo else {}` part the on the first shot (assuming there will be an
empty list when foo='' and that `for` will not be entered at all).

Best,
Mart Sõmermaa



More information about the Python-ideas mailing list