Movatterモバイル変換


[0]ホーム

URL:


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

Josiah Carlsonjcarlson 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.htmlYou apparently haven't been reading python-dev for around 36 hours,because there have been over a dozen positive comments in regards tostr.partition().> 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 elseWhy don't you pause for a second and read Raymond's post here:http://mail.python.org/pipermail/python-dev/2005-August/055781.htmlIn that email there is a listing of standard library translations fromstr.find to str.partition, and in every case, it is improved.  If youbelieve that str.index would be better used, take a moment and do a fewtranslations of the sections provided and compare them with thestr.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 returnedpartition result are used.  The remaining few are generally splitbetween one or two instances.  In the microbenchmarks I've conducted,manually generating the slicings are measureably slower than when Pythondoes it automatically.  Also, exceptions are actually quite slow inrelation 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...0.953000068665>>> if 1:...     x = 'h'...     t = time.time()...     for i in xrange(1000000):...             try:...                     x.index('i')...             except ValueError:...                     pass...     print time.time()-t...6.53100013733I urge you to take some time to read Raymond's translations. - Josiah


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2025 Movatter.jp