[Python-Dev] Dafanging the find() gotcha

Patrick K. O'Brien pobrien@orbtech.com
Mon, 5 Aug 2002 18:27:50 -0500

> >
> > I'm not trying to be flip here -- I'm trying to make the point that in
> > my opinion, having a uniform rule is preferable to catching particular
> > cases that are sometimes mistakes.
> It's not so much that '' in 'abc' is a mistake as that there's no
> sensible answer to be given.  When Python can't figure out how to
> deliver a sensible answer, it raises an exception: "In the face of
> ambiguity, refuse the temptation to guess."

In what way does find('') return a sensible answer?

>>> 'help'.find('')
>>> 'help'.find('h')
>>> 'help'.find('e')
>>> 'help'.find('l')
>>> 'help'[0]
>>> 'help'[1]
>>> 'help'[2]
>>> s = 'help'
>>> s[s.find('')]
>>> s[s.find('h')]

I don't see the logic in this and I couldn't find anything in the docs to
explain this behavior. I'm guessing this is old hat for most of you, but I
find this a bit surprising myself.

Patrick K. O'Brien
"Your source for Python programming expertise."
Web:  http://www.orbtech.com/web/pobrien/
Blog: http://www.orbtech.com/blog/pobrien/
Wiki: http://www.orbtech.com/wiki/PatrickOBrien