str.index() and str.find() versus only list.index()
toddrjen at gmail.com
Tue Jul 14 10:20:41 CEST 2015
On Tue, Jul 14, 2015 at 7:13 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Tue, Jul 14, 2015 at 2:58 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
> > On Tuesday 14 July 2015 14:07, Ian Kelly wrote:
> >> On Mon, Jul 13, 2015 at 9:23 PM, Steven D'Aprano <steve at pearwood.info>
> >> wrote:
> >>> Correct. But rather than removing it, it would be better to take a leaf
> >>> out of re.match's book and return None as the sentinel. That would
> >>> eliminate the "using -1 as a valid index" bug.
> >> Better IMO to just have the one non-redundant method that raises an
> >> exception rather than returning anything that could possibly be
> >> interpreted as a string index.
> > Well, maybe, but if you got rid of str.find, the first thing people
> would do
> > is recreate it:
> > def find(*args):
> > try:
> > return str.index(*args)
> > except ValueError:
> > return -1
> > Having a version of str.index that returns a sentinel is just too damn
> > handy.
> Same as dictionaries have  and .get(), although find doesn't allow
> you to change the sentinel.
Maybe that is the solution? Add a keyword-only argument to find to change
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list