Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sub': 0.09; 'substring': 0.09; 'terry': 0.09; 'index': 0.13; 'cases': 0.15; 'doc,': 0.16; 'index.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 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; 'jan': 0.18; '>>>': 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; 'header:X-Complaints-To:1': 0.28; 'subject:/': 0.28; 'changed.': 0.29; 'optional': 0.29; "skip:' 10": 0.30; 'generally': 0.32; 'could': 0.32; 'int': 0.33; 'raising': 0.33; 'to:addr:python-list': 0.33; 'returning': 0.35; 'sometimes': 0.35; 'there': 0.35; 'received:org': 0.36; '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; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'think': 0.40; 'range': 0.60; 'within': 0.64; 'behavior': 0.64; 'answer.': 0.71; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Date: Wed, 21 Nov 2012 22:41:01 -0500 References: <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 In-Reply-To: <50ACD7FB.3060906@mrabarnett.plus.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353555686 news.xs4all.nl 6962 [2001:888:2000:d::a6]:40178 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33782 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