[Python-Dev] Proof of the pudding: str.partition()
Benji York
benji at benjiyork.com
Tue Aug 30 23:06:06 CEST 2005
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
More information about the Python-Dev
mailing list