<snipping occurs> On 21 November 2012 20:58, MRAB <pyt...@mrabarnett.plus.com> wrote:
> On 2012-11-21 19:25, Hans Mulder wrote: > >> On 21/11/12 17:59:05, Alister wrote: >> >>> On Wed, 21 Nov 2012 04:43:57 -0800, Giacomo Alzetta wrote: >>>> >>>> '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. >>>> >>>> Return -1 on failure. >>> >>> >>> >>> why would you be searching for an empty string? >>> what result would you expect to get from such a search? >>> >> >> In general, if >> >> needle in haystack[ start: ] >> >> return True, then you' expect >> >> haystack.find(needle, start) >> >> to return the smallest i >= start such that >> >> haystack[i:i+len(needle)] == needle >> >> also returns True. >> >> The only other consistent position would be that "spam"[5:] >> should raise an IndexError, because 5 is an invalid index. > > > > You'd expect that given: > > found = string.find(something, start, end) > > if 'something' present then the following are true: > > 0 <= found <= len(string) > > start <= found <= end > I don't think I agree. I think *only* the second holds, but start "defaults" to 0 and end to len(string), which is consistent with Han's method and thus conclusion. Of course this is merely opinion and practicality beats purity, but it seems logical, considering the slicing synonym. Even if functionally this remains unchanged, the docstring should be edited to reflect this oddity. (I'm assuming here that 'start' and 'end' have already been adjusted > for counting from the end, ie originally they might have been negative > values.) > > The only time that you can have found == len(string) and found == end > is when something == "" and start == len(string). >
-- http://mail.python.org/mailman/listinfo/python-list