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:] == ''
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.
-- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
