new string method in 2.5 (partition)
Tim Chase
python.list at tim.thechases.com
Tue Sep 19 15:25:03 EDT 2006
>> partition(sep) condenses this pattern into a single method
>> call that returns a 3-tuple containing the substring before
>> the separator, the separator itself, and the substring after
>> the separator. If the separator isn't found, the first
>> element of the tuple is the entire string and the other two
>> elements are empty. rpartition(sep) also returns a 3-tuple
>> but starts searching from the end of the string; the "r"
>> stands for 'reverse'.
>
> I'm confused. What's the difference between this and
> string.split?
(please don't top-post...I've inverted and trimmed for the sake
of readability)
I too am a bit confused but I can see uses for it, and there
could be good underlying reason to do as much. Split doesn't
return the separator. It's also guarnteed to return a 3-tuple. E.g.
>>> s1 = 'one'
>>> s2 = 'one|two'
>>> len(s1.split('|', 1)
1
>>> len(s2.split('|', 1))
2
which could make a difference when doing tuple-assignment:
>>> v1, v2 = s2.split('|', 1)
>>> # works fine
>>> v1, v2 = s1.split('|', 1)
[traceback]
whereas one could consistently do something like
>>> v1, _, v2 = s1.partition('|')
without fear of a traceback to deal with.
Just a few thoughts...
-tkc
More information about the Python-list
mailing list