Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] Proof of the pudding: str.partition()

Josiah Carlsonjcarlson at uci.edu
Wed Aug 31 03:35:37 CEST 2005


tony at lownds.com wrote:> Substr didn't copy as partition() will have to, won't many of uses of> partition() end up being O(N^2)?Yes.  But if you look at most cases provided for in the standard library,that isn't an issue.  In the case where it becomes an issue, it isgenerally because a user wants to do repeated splitting on the sametoken...which is better suited for str.split or re.split.> One way that gives the programmer a way avoid the copying would be to> provide a string method> findspan(). findspan() would returns the start and end of the found> position in the string. start >> end could signal no match; and since 0-character strings are disallowed in> partition, end == 0> could also signal no match. partition() could be defined in terms of> findspan():> start, end = s.findspan(sep)> before, sep, after = s[:start], s[start:end], s[end:]Actually no.  When str.parition() doesn't find the separator, you get s, '', ''. Yours would produce '', '', s.  On not found, you would need to usestart==end==len(s).Further, findspan could be defined in terms of find...def findspan(s, sep):    if len(sep) == 0:        raise ValueError("null separator strings are not allowed")    x = s.find(sep)    if x >= 0:        return x, x+len(sep)    return len(s),len(s)Conceptually they are all the same.  The trick with partition is that inthe vast majority of use cases, one wants 2 or 3 of the resultingstrings, and constructing the strings in the C-level code is far fasterthan manually slicing (which can be error prone).  I will say the samething that I've said at least three times already (with a bit of anexpansion):    IF YOU ARE GOING TO PROPOSE AN ALTERNATIVE, SHOW SOME    COMPARATIVE CODE SAMPLES WHERE YOUR PROPOSAL DEFINITELY    WINS OVER BOTH str.find AND str.partition.  IF YOU CAN'T    PROVIDE SUCH SAMPLES, THEN YOUR PROPOSAL ISN'T BETTER,    AND YOU PROBABLY SHOULDN'T PROPOSE IT.  bonus points for    those who take the time to compare all of those that    Raymond provided. - Josiah


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2025 Movatter.jp