473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Difficulty with maxsplit default value for str.split

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-me...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,

--
Steven.

Sep 24 '06 #1
4 2498
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
---------------------
result=S.split(sep) if maxsplit is None else S.split(sep,maxsplit)
---------------------

-Nick Vatamaniuc

Steven D'Aprano wrote:
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-me...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,

--
Steven.
Sep 24 '06 #2
Steven D'Aprano wrote:
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>

Sep 24 '06 #3
On Sun, 24 Sep 2006 10:34:31 +0200, Fredrik Lundh wrote:
>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
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.
--
Steven D'Aprano

Sep 25 '06 #4
Steven D'Aprano <st***@REMOVE.THIS.cybersource.com.auwrites:
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.
Sep 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sosman | last post by:
I take it python doesn't support defaults when assigning to a tuple, eg: for line in file: (parameter, value, units = 'kg') = line.split() along the lines of default parameter assignment in...
13
by: TB | last post by:
Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: >>> a, b, c = (line.split(':')) if line could have less than three...
2
by: Antoon Pardon | last post by:
I was wondering what people whould think about a change of behaviour in the split method fo strings. The idea would be that if maxsplit was negative then abs(maxsplit) splits would be done, but...
1
by: Tim Marshall | last post by:
I've just received a new computer with Win XP Version 5.1.2600. I want to have Access 2003 and Access 97 running on this, so I did some googling on cdma and it seems I must install these versions...
0
by: Anonieko Ramos | last post by:
Answer. Use IHttpHandler. thanks Ro ry for coming up with this code. It processes css file to add variables. neat idea using System; using System.IO; using System.Text; using...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
4
by: dk9 | last post by:
The Situation: I'm working in my browser at a secured site (https). At the same time I need my application to get and parse some urls from that same site. What I've tried: I've login with my...
2
by: preityrao | last post by:
Hi, I am new to perl and was trying to write a perl program witha UI. i.e. I have written a html form which invokes a perl program to execute and to print back the data selected in the form. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.