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

Fredrik Lundh fredrik at pythonware.com
Wed Aug 31 16:03:29 CEST 2005

Phillip J. Eby wrote:

> Yep, subscripting and slicing are more than adequate to handle *all* of
> those use cases, even the ones that some people have been jumping through
> odd hoops to express:
>     before = x.partition(sep)[0]
>     found  = x.partition(sep)[1]
>     after  = x.partition(sep)[2]
>     before, found = x.partition("foo")[:2]
>     found,  after = x.partition("foo")[1:]
>     before, after = x.partition("foo")[::2]
> Okay, that last one is maybe a little too clever.  I'd personally just use
> '__' or 'DONTCARE' or something like that for the value(s) I didn't care
> about, because it  actually takes slightly less time to unpack a 3-tuple
> into three function-local variables than it does to pull out a single
> element of the tuple, and it's almost twice as fast as taking a slice and
> unpacking it into two variables.

you're completely missing the point.

the problem isn't the time it takes to unpack the return value, the problem is that
it takes time to create the substrings that you don't need.

for some use cases, a naive partition-based solution is going to be a lot slower
than the old find+slice approach, no matter how you slice, index, or unpack the
return value.

> So, using three variables is both faster *and* easier to read than any of
> the variations anybody has proposed, including the ones I just showed above.

try again.


More information about the Python-Dev mailing list