tags:

views:

1120

answers:

5

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?

+11  A: 

The string.split(stringobj) is a feature of the string module, 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 the string module. But pretty old, nonetheless. That's the current practice.

S.Lott
so when I use str.split(), Why the eclipse don't do the auto completion?
UcanDoIt
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.
S.Lott
+18  A: 

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).

csl
+1: teach a person to fish
S.Lott
+3  A: 

An extra note: str is 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.split is the unbound method, while s.split is a bound method of a str object. In the second case, the string that gets passed in to str.split is used as self in the method.

This doesn't make much difference here, but it's an important feature of how Python's object system works.

More info about bound and unbound methods.

wilberforce
A: 

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.

Jason Baker
+4  A: 

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.