Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93771

Re: str.index() and str.find() versus only list.index()

References <mo0qk1$ora$1@ger.gmane.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-07-13 21:12 -0600
Subject Re: str.index() and str.find() versus only list.index()
Newsgroups comp.lang.python
Message-ID <mailman.475.1436843580.3674.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Jul 13, 2015 at 10:56 AM, Roel Schroeven <roel@roelschroeven.net> wrote:
> Hi,
>
> Quick question: why does str have both index() and find(), while list only
> has index()? Is there a reason for that, or is it just an historical
> accident?

Historical accident, I think. If it were to be redone, I doubt that
str.find would exist. The problem with it is that it returns -1 to
indicate that the argument was not found, but -1 is a valid index into
the string. This is a potential source of hard-to-find bugs.

There was a long thread from 2005 on python-dev proposing to remove
str.find in Python 3, but evidently it didn't make the cut:
https://mail.python.org/pipermail/python-dev/2005-August/055704.html

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: str.index() and str.find() versus only list.index() Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-13 21:12 -0600
  Re: str.index() and str.find() versus only list.index() Steven D'Aprano <steve@pearwood.info> - 2015-07-14 13:23 +1000
    Re: str.index() and str.find() versus only list.index() Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-13 22:07 -0600
      Re: str.index() and str.find() versus only list.index() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-07-14 14:58 +1000
        Re: str.index() and str.find() versus only list.index() Chris Angelico <rosuav@gmail.com> - 2015-07-14 15:13 +1000

csiph-web