On 2012-11-22 03:41, Terry Reedy wrote:
On 11/21/2012 8:32 AM, MRAB wrote:
On 2012-11-21 12:43, Giacomo Alzetta wrote:
I just came across this:

  >>> 'spam'.find('')
0
  >>> 'spam'.find('', 1)
1
  >>> 'spam'.find('', 4)
4

'spam'.find('', 5)
-1


Now, reading find's documentation:

print(str.find.__doc__)
S.find(sub [,start [,end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

This seems not to be true, as 'spam'[4:] == 'spam'[5:] == ''

It can't return 5 because 5 isn't an index in 'spam'.

It can't return 4 because 4 is below the start index.

Return -1 on failure.

Now, the empty string is a substring of every string so how can find
fail?
find, from the doc, should be generally be equivalent to
S[start:end].find(substring) + start, except if the substring is not
found but since the empty string is a substring of the empty string it
should never fail.

[snip]
I think that returning -1 is correct (as far as returning -1 instead of
raising an exception like .index could be considered correct!) because
otherwise it whould be returning a non-existent index. For the string
"spam", the range is 0..4.

I tend to agree, but perhaps the doc should be changed. In edge cases
like this, there sometimes is no 'right' answer. I suspect that the
current behavior is intentional. You might find a discussion on the tracker.


It's a special case, but the Zen has something to say about that! :-)

(The empty string is also the only substring which can start at len(S).)

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to