[Python-Dev] partition() (was: Remove str.find in 3.0?)

Josiah Carlson jcarlson at uci.edu
Tue Aug 30 10:42:22 CEST 2005

Pierre Barbier de Reuille <pierre.barbier at cirad.fr> wrote:
> Well, I want to come back on a point that wasn't discussed. I only found
> one positive comment here :
> http://mail.python.org/pipermail/python-dev/2005-August/055775.html

You apparently haven't been reading python-dev for around 36 hours,
because there have been over a dozen positive comments in regards to

> Raymond Hettinger wrote:
> > * The function always succeeds unless the separator argument is not a
> > string type or is an empty string.  So, a typical call doesn't have to
> > be wrapped in a try-suite for normal usage.
> Well, I wonder if it's so good ! Almost all the use case I find would
> require something like:
> head, sep, tail = s.partition(t)
> if sep:
>    do something
> else:
>    do something else

Why don't you pause for a second and read Raymond's post here:

In that email there is a listing of standard library translations from
str.find to str.partition, and in every case, it is improved.  If you
believe that str.index would be better used, take a moment and do a few
translations of the sections provided and compare them with the
str.partition examples.

> Like, if you want to extract the drive letter from a windows path :
> drive, sep, tail = path.partition(":")
> if not sep:
>    drive = get_current_drive() # Because it's a local path
> Or, if I want to iterate over all the path parts in a UNIX path:
> sep = '/'
> while sep:
>   head, sep, path = path.partition(sep)
> IMO, that read strange ... partitionning until sep is None :S
> Then, testing with "if" in Python is always a lot slower than having an
> exception launched from C extension inside a try...except block.

In the vast majority of cases, all three portions of the returned
partition result are used.  The remaining few are generally split
between one or two instances.  In the microbenchmarks I've conducted,
manually generating the slicings are measureably slower than when Python
does it automatically.  Also, exceptions are actually quite slow in
relation to comparisons, specifically in the case of find vs. index
(using 2.4)...

>>> if 1:
...     x = 'h'
...     t = time.time()
...     for i in xrange(1000000):
...             if x.find('i')>=0:
...                     pass
...     print time.time()-t
>>> if 1:
...     x = 'h'
...     t = time.time()
...     for i in xrange(1000000):
...             try:
...                     x.index('i')
...             except ValueError:
...                     pass
...     print time.time()-t

I urge you to take some time to read Raymond's translations.

 - Josiah

More information about the Python-Dev mailing list