Issue1106694
Created on 2005-01-21 13:30 by boukthor, last changed 2005-01-25 10:42 by rhettinger. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg24018 - (view) | Author: Vinz (boukthor) | Date: 2005-01-21 13:30 | |
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
|
|||
| msg24019 - (view) | Author: Bastian Kleineidam (calvin) | Date: 2005-01-24 12:34 | |
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. |
|||
| msg24020 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2005-01-25 10:42 | |
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). |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2005-01-21 13:30:04 | boukthor | create | |
