Bugs item #1106694, was opened at 2005-01-21 08:30 Message generated for change (Comment added) made by rhettinger You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1106694&group_id=5470
Category: Python Interpreter Core Group: Not a Bug >Status: Closed >Resolution: Invalid Priority: 5 Submitted By: Vinz (boukthor) Assigned to: Nobody/Anonymous (nobody) Summary: split() takes no keyword arguments Initial Comment: I'm running python 2.4 on a Linux box (Mandrake cooker 10.2). Since the string functions have been implemented as method of string instances, there's little use for the string module (except for constants). However, the behaviour of the 'split' method is slightly different from the one in the string module : it does not accept keyword arguments. This is annoying because the default separator argument ('any whitespace') cannot be provided manually (at least I couldn't find a way to do it) and that means that you can not use the default separator while specifying a maxsplit argument (if you specify maxsplit, you need to give a separator manually because it comes first in the arg list), unless you use "string.split" (and "import string") syntax. Examples : >>> "foo bar\tbaz\nqux".split() ['foo', 'bar', 'baz', 'qux'] >>> string.split("foo bar\tbaz\nqux") ['foo', 'bar', 'baz', 'qux'] >>> "foo bar\tbaz\nqux".split(" \t\n") # this is ok, just illustrating that you cannot emulate the default behaviour if you provide a separator manually ['foo bar\tbaz\nqux'] >>> string.split("foo bar\tbaz\nqux", maxsplit=2) # what I want to do ['foo', 'bar', 'baz\nqux'] >>> "foo bar\tbaz\nqux".split(maxsplit=2) # what I get Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: split() takes no keyword arguments >>> "foo bar\tbaz\nqux".split(2) # cannot skip the sep arg Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: expected a character buffer object ---------------------------------------------------------------------- >Comment By: Raymond Hettinger (rhettinger) Date: 2005-01-25 05:42 Message: Logged In: YES user_id=80475 As Calvin points out, the docs for str.split are clear about being able to write None to get the default any whitespace option. As a for making maxsplit a keyword argument, that might have been considered many years ago when string methods were introduced. At this point, that is ancient history and not worth mucking-up the API by introducing yet another inter-version incompatability (works in Py2.5 but not in Py2.2, 2.3, or 2.4). ---------------------------------------------------------------------- Comment By: Wummel (calvin) Date: 2005-01-24 07:34 Message: Logged In: YES user_id=9205 Specifying None as separator gives you (as documented) the split-on-mulitple-whitespace behaviour. This applies to both string.split() and the split() string method. So skipping the separator arg is not needed. However, I agree to your wish that the string method split() should accept the "maxsplit" as a keyword argument. It is more backward compatible to the old string.split() function that way. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1106694&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com