[ python-Bugs-911080 ] string str.split() behaviour inconsistency

SourceForge.net noreply at sourceforge.net
Sun Mar 7 02:23:39 EST 2004


Bugs item #911080, was opened at 2004-03-06 15:41
Message generated for change (Settings changed) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=911080&group_id=5470

Category: Python Interpreter Core
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Pete Shinners (peteshinners)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: string str.split() behaviour inconsistency

Initial Comment:
The str.split() method behaves differently depending on
if it uses the default (no arguments) separator, or if
you provide your own. There is no way to reproduce the
functionality of the default separator if you supply
your own.

>>> s = "a b  c"
>>> s.split()
['a', 'b', 'c']
>>> s.split(" ")
['a', 'b', '', 'c']

The default split uses a different algorithm, where it
combines multiple separators into a single separator.
Providing a custom separator makes split separate each
individual separator.

Obviously there are good reasons for forcing a separate
entry between each separator. With simple comma or
colon separated records, you want to know if an entry
is blank.

The problem is there is not a way to reproduce the
functionality of the default behavior. This alternate
behavior is also not documented, so it becomes
confusing why split behaves different once you want
your own separators.

Fixing could be a problem. Changing the actual split()
method would break many programs. But adding an
different split is a potentially nice solution.

The other option would be to "re-use" the current
splitfields() function and have it work like the
current split. And change the split() to behave like it
does with no default. This would unfortunately still
"break stuff".

The easiest fix may just be documentation and letting
people know of this difference.

I've been helping some newbies through Python. When
this came up I was a little surprised and we were
forced to learn it was just a little "magic and scary".


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=911080&group_id=5470



More information about the Python-bugs-list mailing list