[Python-Dev] Dafanging the find() gotcha

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


[Aahz]
> >
> > 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('')
0
>>> 'help'.find('h')
0
>>> 'help'.find('e')
1
>>> 'help'.find('l')
2
>>> 'help'[0]
'h'
>>> 'help'[1]
'e'
>>> 'help'[2]
'l'
>>> s = 'help'
>>> s[s.find('')]
'h'
>>> s[s.find('h')]
'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
Orbtech
-----------------------------------------------
"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
-----------------------------------------------