Hi,
There is one thing that I do not understand...
Imagine you have a text = "hello world" and you want to split it.
In some places I see people that want to split the text doing:
string.split(text)
In other places I see people just doing:
text.split()
What’s the difference? Why you do in one way or in the other way? Can you give me a theorist explanation about that?
-
The
string.split(stringobj)is a feature of thestringmodule, which must be imported separately. Once upon a time, that was the only way to split a string. That's some old code you're looking at.The
stringobj.split()is a feature of a string object,stringobj, which is more recent than thestringmodule. But pretty old, nonetheless. That's the current practice.UcanDoIt : so when I use str.split(), Why the eclipse don't do the auto completion?S.Lott : Because it can't figure out that `str` is a string and has appropriate methods like 'split()`.S.Lott : Also, if you're literally saying `str`, that's a built-in function, and that will also leads to additional confusion. If you're saying "someStringObj", then that's Eclipse's inability to reason out the type of the variable. -
Interestingly, the docstrings for the two are not completely the same in Python 2.5.1:
>>> import string >>> help(string.split) Help on function split in module string: split(s, sep=None, maxsplit=-1) split(s [,sep [,maxsplit]]) -> list of strings Return a list of the words in the string s, using sep as the delimiter string. If maxsplit is given, splits at no more than maxsplit places (resulting in at most maxsplit+1 words). If sep is not specified or is None, any whitespace string is a separator. (split and splitfields are synonymous) >>> help("".split) Help on built-in function split: split(...) S.split([sep [,maxsplit]]) -> list of strings Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator.Digging deeper, you'll see that the two forms are completely equivalent, as string.split(s) actually calls s.split() (search for the split-functions).
S.Lott : +1: teach a person to fish -
An extra note:
stris the string type, as S.Lott points out above. That means that these two forms:'a b c'.split() str.split('a b c') # both return ['a', 'b', 'c']...are equivalent, because
str.splitis the unbound method, whiles.splitis a bound method of astrobject. In the second case, the string that gets passed in tostr.splitis used asselfin the method.This doesn't make much difference here, but it's an important feature of how Python's object system works.
-
Use whichever you like, but realize that str.split is the recommended way of doing it. :-)
string.split is a tad older method of doing the same thing.
str.split is a bit more efficient (since you don't have to import the string module or look up any names from it), but not enough to make a huge difference if you prefer string.split.
-
Short answer: the string module was the only way to perform these operations before python 1.6 - they've since been added to strings as methods.
0 comments:
Post a Comment