Small inconsistency between string.split and "".split

Erik Heneryd erik at heneryd.com
Mon Sep 13 13:10:24 EDT 2004


Carlos Ribeiro wrote:
> Hi all,
> 
> While writing a small program to help other poster at c.l.py, I found
> a small inconsistency between the handling of keyword parameters of
> string.split() and the split() method of strings. I wonder if someone
> else had ever stumbled on it before, and if it has a good reason to
> work like it is.
> 
> Both implementations take two parameters: the separator character and
> the max number of splits (maxsplit). However, string.split() accept
> maxsplit as a keyword parameter, while mystring.split() doesn't. In my
> case, it meant that I had to resort to string.split() in my example,
> in order to avoid having to deal with the separator.
> 
> ** BTW, I had to avoid dealing with the separator for another annoying
> reason: I thought that I could do something like this:
> 
> mystring.split(string.whitespace, 2)
> 
> to preserve the default whitespace detecting behavior. But it won't
> work this way with neither implementation of split().

1) If you look at the string lib docs, the parameters are meant to be 
positional, not keywords...

2) You can get the same behavior from str.split() by using None as the 
separator (undocumented, I guess).

IMHO both the string function (just a matter of documentation) and the 
str object method should accept keywords.


Erik



More information about the Python-list mailing list