Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!news1.as3257.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'true,': 0.04; 'mrab': 0.05; 'arguments': 0.07; 'interpreted': 0.07; 'agree,': 0.09; 'failure.': 0.09; 'notation.': 0.09; 'sub': 0.09; 'substring': 0.09; 'terry': 0.09; 'index': 0.13; ':-)': 0.13; '(the': 0.15; 'cases': 0.15; 'doc,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'index.': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:parameters': 0.16; 'subject:when': 0.16; 'string': 0.17; 'wrote:': 0.17; 'found,': 0.17; 'tend': 0.17; '>>>': 0.18; 'discussion': 0.20; 'equivalent': 0.20; 'doc': 0.22; 'this:': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'am,': 0.27; '(as': 0.27; 'start,': 0.27; 'correct': 0.28; 'subject:/': 0.28; 'changed.': 0.29; 'optional': 0.29; 'received:192.168.1.3': 0.29; 'case,': 0.29; "skip:' 10": 0.30; 'generally': 0.32; 'could': 0.32; 'int': 0.33; 'raising': 0.33; 'that!': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'returning': 0.35; 'sometimes': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'far': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'end': 0.40; 'think': 0.40; 'range': 0.60; 'within': 0.64; 'behavior': 0.64; 'header:Reply-To:1': 0.68; 'answer.': 0.71; 'reply-to:no real name:2**0': 0.72; 'special': 0.73; 'reply-to:addr:python.org': 0.84; 'zen': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=TdYURGsh c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=fHJ554mUI9MA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=JLrdG8ww-nQA:10 a=q4Yj-kdWnoPGBLC-1AUA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 22 Nov 2012 04:00:38 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Inconsistent behaviour os str.find/str.index when providing optional parameters References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353556838 news.xs4all.nl 6975 [2001:888:2000:d::a6]:44753 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33783 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).)