Difficulty with maxsplit default value for str.split

S

Steven D'Aprano

I'm having problems passing a default value to the maxsplit argument of
str.split. I'm trying to write a function which acts as a wrapper to
split, something like this:

def mysplit(S, sep=None, maxsplit=None):
pre_processing()
result = S.split(sep, maxsplit)
post_processing()
return result

But the split method doesn't accept a value of None for maxsplit, and I
don't know what default value I should be using. Passing 0 as the default
isn't correct, because then it splits zero times.

By experimentation, I have discovered that passing -1 as the default
instead of None *appears* to work, but I'm not sure if I can rely on it or
if that is an accidental implementation detail. According to the
documentation at
http://docs.python.org/lib/string-methods.html#string-methods

split([sep [,maxsplit]])
Return a list of the words in the string, using sep as the delimiter
string. If maxsplit is given, at most maxsplit splits are done. (thus,
the list will have at most maxsplit+1 elements). If maxsplit is not
specified, then there is no limit on the number of splits (all
possible splits are made).


If I take that literally, then the correct way to wrap split is something
like this:

def mysplit(S, sep=None, maxsplit=None):
pre_processing()
if maxsplit is None:
# don't specify maxsplit
result = S.split(sep)
else:
result = S.split(sep, maxsplit)
post_processing()
return result

Is it safe for me to pass -1 as the default to maxsplit, meaning
"unlimited splits"? Should the docs be fixed to mention that?

Thanks,
 
N

Nick Vatamaniuc

Steven,

According to the current Python source the default value of
maxsplit=-1. I think you can count on that as I don't think there would
be a reason to change it in the future. If you are really worried about
it then your version of calling a particular version of split should
work.

By the way, if you use Python 2.5 and when your maxsplit function is
called there is a clear common case in regards to maxsplit, you could
use the new conditional expression. Say most of the time mysplit is
used with a default value of maxsplit, then you can re-write your 'if'
code as
 
F

Fredrik Lundh

Steven said:
I'm having problems passing a default value to the maxsplit argument of
str.split. I'm trying to write a function which acts as a wrapper to
split, something like this:

def mysplit(S, sep=None, maxsplit=None):
pre_processing()
result = S.split(sep, maxsplit)
post_processing()
return result

But the split method doesn't accept a value of None for maxsplit, and I
don't know what default value I should be using.

def mysplit(S, *args):
pre_processing()
result = S.split(*args)
post_processing()
return result
Is it safe for me to pass -1 as the default to maxsplit, meaning
"unlimited splits"?

not really.

</F>
 
S

Steven D'Aprano

def mysplit(S, *args):
pre_processing()
result = S.split(*args)
post_processing()
return result

Thanks Fredrik, that's *exactly* the sort of insight I was lacking. And
now that you've shown me, I can't believe how obvious it is.
 
P

Paul Rubin

Steven D'Aprano said:
Is it safe for me to pass -1 as the default to maxsplit, meaning
"unlimited splits"? Should the docs be fixed to mention that?

Frederik gave a simple and practical solution to your immediate
problem, but yes, I think the docs should be fixed after checking the
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top