[Python-ideas] str.split() oddness
Steven D'Aprano
steve at pearwood.info
Mon Mar 7 00:21:52 CET 2011
On Mon, 7 Mar 2011 08:06:08 am Mart Sõmermaa wrote:
> 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 {})
>
> ?
Which would you prefer?
line = line.split("#")[0].rstrip()
line = line.split("#")[0].rstrip() if line else ""
Whichever behaviour we give split, we're going to complicate something.
Since there's no overwhelming reason to prefer one use-case over the
other, the status quo wins. Any change to the behaviour of split will
break code which is currently working, and that alone is reason enough
to stick with the current behaviour.
By the way, your dict() examples are not robust against minor whitespace
changes in foo. Consider what happens with either of:
foo = "x=1, y = 4, z = 2"
foo = "x=1,y=4,z=2,"
--
Steven D'Aprano
More information about the Python-ideas
mailing list