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

Raymond Hettinger raymond.hettinger at verizon.net
Tue Aug 30 22:27:48 CEST 2005


[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))

The remaining cases don't bug me much.  They clearly say, ignore the
left piece or ignore the right piece.  We could, of course, make these
clearer and more efficient by introducing more methods:
   
   s.before(sep)  --> (left, sep)
   s.after(sep)   --> (right, sep)
   s.rbefore(sep) --> (left, sep)
   s.r_after(sep) --> (right, sep)

But who wants all of that?


Raymond



More information about the Python-Dev mailing list