Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33735

Re: Inconsistent behaviour os str.find/str.index when providing optional parameters

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'arguments': 0.07; 'interpreted': 0.07; 'failure.': 0.09; 'notation.': 0.09; 'sub': 0.09; 'substring': 0.09; 'index': 0.13; '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; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject: \n ': 0.16; 'subject:parameters': 0.16; 'subject:when': 0.16; 'string': 0.17; 'wrote:': 0.17; 'found,': 0.17; 'equivalent': 0.20; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(as': 0.27; 'start,': 0.27; 'correct': 0.28; 'subject:/': 0.28; '>>>>': 0.29; 'optional': 0.29; 'received:192.168.1.3': 0.29; "skip:' 10": 0.30; 'generally': 0.32; 'received:84': 0.32; 'could': 0.32; 'int': 0.33; 'raising': 0.33; 'to:addr:python-list': 0.33; 'returning': 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; 'header:Reply-To:1': 0.68; 'reply- to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=BusfMPr5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=1EKJ2blYo8kA: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=6U5SglhgzSUDhjMW-xQA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Wed, 21 Nov 2012 13:32:43 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121026 Thunderbird/16.0.2
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>
In-Reply-To <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.153.1353504767.29569.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1353504767 news.xs4all.nl 6895 [2001:888:2000:d::a6]:57973
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33735

Show key headers only | View raw


On 2012-11-21 12:43, Giacomo Alzetta wrote:
> I just came across this:
>
>>>> '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.
>
> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Inconsistent behaviour os str.find/str.index when providing optional parameters Giacomo Alzetta <giacomo.alzetta@gmail.com> - 2012-11-21 04:43 -0800
  Re: Inconsistent behaviour os str.find/str.index when providing optional parameters MRAB <python@mrabarnett.plus.com> - 2012-11-21 13:32 +0000
  Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Alister <alister.ware@ntlworld.com> - 2012-11-21 16:59 +0000
    Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Hans Mulder <hansmu@xs4all.nl> - 2012-11-21 20:25 +0100
      Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Giacomo Alzetta <giacomo.alzetta@gmail.com> - 2012-11-21 12:21 -0800
      Re: Inconsistent behaviour os str.find/str.index when providing optional parameters MRAB <python@mrabarnett.plus.com> - 2012-11-21 20:58 +0000
  Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Terry Reedy <tjreedy@udel.edu> - 2012-11-21 22:41 -0500
  Re: Inconsistent behaviour os str.find/str.index when providing optional parameters MRAB <python@mrabarnett.plus.com> - 2012-11-22 04:00 +0000
    Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Giacomo Alzetta <giacomo.alzetta@gmail.com> - 2012-11-21 23:01 -0800
      Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-22 08:44 +0000
        Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Giacomo Alzetta <giacomo.alzetta@gmail.com> - 2012-11-22 10:22 -0800
    Re: Inconsistent behaviour os str.find/str.index when providing optional parameters Giacomo Alzetta <giacomo.alzetta@gmail.com> - 2012-11-21 23:01 -0800

csiph-web