473,587 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2509
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,max split)
---------------------

-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.T HIS.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
4945
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 function calls.
13
2641
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 fields? Thanks, TB
2
1959
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 splitting would start from the right instead of the left. Now we have. >>> "st1:st2:st3:st4:st5".split(':',2)
1
43917
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 of Access "sequentially", ie, A97, first. When I use the Office 97 installation cd, pick my choices, I end up, at the end of the installation, an...
0
1885
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 System.Text.RegularExpressions; using System.Web; using System.Web.Caching;
14
3256
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 make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and...
4
6125
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 extra item just doesn't get added to the DropDownList: ============================================= <script runat="server"> Sub...
4
2455
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 browser, but when trying to get the pages using WebRequest from my application I can only get the "you need to login page". I've then put a...
2
1705
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. When i enter details into the html form and click on the submittal button, it throws an error saying "Internal Server Error The server encountered...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7973
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.