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

Ron Adam rrr at ronadam.com
Wed Aug 31 00:45:54 CEST 2005


Benji York wrote:

> Raymond Hettinger wrote:
> 
>>[Fredrik Lundh]
>>
>>
>>>it is, however, a bit worrying that you end up ignoring one or more
>>>of the values in about 50% of your examples...
>>
>>It drops to about 25% when you skip the ones that don't care about the
>>found/not-found field:
>>
>>
>>>>!     _, sep, port = host.partition(':')
>>>>!             head, sep, _ = path.rpartition('/')
>>>>!                 line, _, _ = line.partition(';')  # strip
>>>>!         pname, found, _ = pname.rpartition('.')
>>>>!             line, _, _ = line.partition('#')
>>>>!         filename, _, _ = filename.partition(chr(0))
> 
> 
> I know it's been discussed in the past, but this makes me wonder about 
> language support for "dummy" or "don't care" placeholders for tuple 
> unpacking.  Would the above cases benefit from that, or (as has been 
> suggested in the past) should slicing be used instead?
> 
> Original:
>       _, sep, port = host.partition(':')
>       head, sep, _ = path.rpartition('/')
>       line, _, _ = line.partition(';')
>       pname, found, _ = pname.rpartition('.')
>       line, _, _ = line.partition('#')
> 
> Slicing:
>       sep, port = host.partition(':')[1:]
>       head, sep = path.rpartition('/')[:2]
>       line = line.partition(';')[0]
>       pname, found = pname.rpartition('.')[:2]
>       line = line.partition('#')[0]
> 
> I think I like the original better, but can't use "_" in my code because 
> it's used for internationalization.
> --
> Benji York

For cases where single values are desired, attribues could work.

Slicing:
        line = line.partition(';').head
        line = line.partition('#').head

But it gets awkward as soon as you want more than one.

        sep, port = host.partition(':').head, host.partition(':').sep


Ron








More information about the Python-Dev mailing list