[Python-Dev] Remove str.find in 3.0?

Andrew Durdin adurdin at gmail.com
Wed Aug 31 04:23:08 CEST 2005


On 8/31/05, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:
> [Hye-Shik Chang]
> > What would be a result for rpartition(s, '?') ?
> > ('', '', 'http://www.python.org')
> > or
> > ('http://www.python.org', '', '')
> 
> The former.  The invariants for rpartition() are a mirror image of those
> for partition().

Just to put my spoke in the wheel, I find the difference in the
ordering of return values for partition() and rpartition() confusing:

head, sep, remainder = partition(s)
remainder, sep, head = rpartition(s)

My first expectation for rpartition() was that it would return exactly
the same values as partition(), but just work from the end of the
string.

IOW, I expected "www.python.org".partition("python") to return exactly
the same as "www.python.org".rpartition("python")

To try out partition(), I wrote a quick version of split() using
partition, and using partition() was obvious and easy:

def mysplit(s, sep):
    l = []
    while s:
        part, _, s = s.partition(sep)
        l.append(part)
    return l

I tripped up when trying to make an rsplit() (I'm using Python 2.3),
because the return values were in "reverse order"; I had expected the
only change to be using rpartition() instead of partition().

For a second example: one of the "fixed stdlib" examples that Raymond
posted actually uses rpartition and partition in two consecutive lines
-- I found this example not immediately obvious for the above reason:

      def run_cgi(self):
         """Execute a CGI script."""
         dir, rest = self.cgi_info
         rest, _, query = rest.rpartition('?')
         script, _, rest = rest.partition('/')
         scriptname = dir + '/' + script
         scriptfile = self.translate_path(scriptname)
         if not os.path.exists(scriptfile):

Anyway, I'm definitely +1 on partition(), but -1 on rpartition()
returning in "reverse order".

Andrew


More information about the Python-Dev mailing list