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

Ron Adam rrr at ronadam.com
Fri Dec 12 03:20:51 CET 2008



Bruce Leban wrote:
> -inf
> 
> That breaks existing code in two different ways which I don't think 
> makes it easy.

Correct, it would break existing code.  Which is why it should have  a 
different name rather than altering the existing split function.


> it does NOT collapse adjacent characters:
>         >>> "a&&b".split("&")
>         ['a', '', 'b']

Also correct.  But that is the behavior when splitting on the default white 
space. ie.. split() with no argument.  '   '.split() is not the same as
'   '.split(' ').

Q: Would it be good to have a new method or function which extends the same 
behavior of whitespace splitting to other user specified characters?

I would find it useful at times.


> the separator it splits on is a string, not a character:
>         >>> "a<b><c>d".split("><")
>         ['a<b', 'c>d']

Yes, I know.  To split on multiple chars in a given argument string it will 
  need to be called something other than .split(). Such as .splitchars(), 
as in the example equality I gave.

     longstring.splitchars(string.whitespace)  ==  longstring.split()

Note: longstring.split() has no arguments.  .split(arg) splits on a string 
     as you stated.



> --- Bruce
> 
> On Thu, Dec 11, 2008 at 4:38 PM, Ron Adam 
> <rrr at ronadam.com 
> <mailto:rrr at ronadam.com>> wrote:
> 
> 
> 
>     Bruce Leban wrote:
> 
>         I think string.split(list) probably won't do what people expect
>         either. Here's what I would expect it to do:
> 
>          >>> '1 (123) 456-7890'.split([' ', '(', ')', '-'])
>         ['1', '', '123', '', '456', '7890']
> 
>         but what you probably want is:
> 
>          >>>re.split(r'[ ()-]*', '1 (123) 456-7890')
>         ['1', '123', '456', '7890']
> 
>         using allows you to do that and avoids ambiguity about what it does.
> 
>         --- Bruce
> 
> 
>     Without getting into regular expressions, it's easier to just allow
>     adjacent char matches to act as one match so the following is true.
> 
>        longstring.splitchars(string.whitespace)  =  longstring.split()
> 
> 
> 
>     _______________________________________________
>     Python-ideas mailing list
>     Python-ideas at python.org
>     <mailto:Python-ideas at python.org>
>     http://mail.python.org/mailman/listinfo/python-ideas
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas




More information about the Python-ideas mailing list