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


Groups > comp.lang.python > #33790

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

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <giacomo.alzetta@gmail.com>
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; 'mrab': 0.05; '-1,': 0.09; 'behave': 0.09; 'indexes': 0.09; 'sentence': 0.09; 'sub': 0.09; 'substring': 0.09; 'terry': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'index': 0.13; '*why*': 0.16; 'docstring': 0.16; 'index.': 0.16; 'indexerror:': 0.16; 'notation,': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:parameters': 0.16; 'subject:when': 0.16; 'string': 0.17; 'wrote:': 0.17; 'integer': 0.17; "shouldn't": 0.17; '(in': 0.18; '>>>': 0.18; '"",': 0.22; 'either.': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'creating': 0.26; '(most': 0.27; 'checking': 0.27; 'correct': 0.28; 'subject:/': 0.28; 'received:209.85.212': 0.28; 'behaviour': 0.29; 'case,': 0.29; 'maybe': 0.29; "skip:' 10": 0.30; 'returned': 0.30; 'file': 0.32; 'could': 0.32; 'traceback': 0.33; 'true.': 0.33; 'equal': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'returning': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'where': 0.40; 'end': 0.40; 'think': 0.40; 'range': 0.60; 'first': 0.61; 'maybe,': 0.84; 'novembre': 0.84; 'received:209.85.212.56': 0.91
Newsgroups comp.lang.python
Date Wed, 21 Nov 2012 23:01:47 -0800 (PST)
In-Reply-To <mailman.190.1353556838.29569.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=158.110.95.152; posting-account=uBCs_goAAABnqb0dwcS_m7_qp7XDdB0G
References <9ecd357d-aaaa-4f4d-a987-a478e92b2052@googlegroups.com> <50ACD7FB.3060906@mrabarnett.plus.com> <k8k6sn$h47$1@ger.gmane.org> <mailman.190.1353556838.29569.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 158.110.95.152
MIME-Version 1.0
Subject Re: Inconsistent behaviour os str.find/str.index when providing optional parameters
From Giacomo Alzetta <giacomo.alzetta@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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>
Message-ID <mailman.196.1353567717.29569.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1353567717 news.xs4all.nl 6966 [2001:888:2000:d::a6]:38264
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33790

Show key headers only | View raw


Il giorno giovedì 22 novembre 2012 05:00:39 UTC+1, MRAB ha scritto:
> On 2012-11-22 03:41, Terry Reedy wrote:
> 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.

Uhm. Maybe you are right, because returning a greater value would cause an IndexError, but then, *why* is 4 returned???

>>> 'spam'.find('', 4)
4
>>> 'spam'[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

4 is not a valid index either. I do not think the behaviour was completely intentional. If find should return indexes than 'spam'.find('', 4) must be -1, because 4 is not a valid index. If find should behave as if creating the slice and checking if the substring is in the slice than 'spam'.find('', i) should return i for every integer >= 4.

The docstring does not describe this edge case, so I think it could be improved.
If the first sentence(being an index in S) is kept, than it shouldn't say that start and end are treated as in slice notation, because that's actually not true. It should be added if start is greater or equal to len(S) then -1 is always returned(and in this case 'spam'.find('', 4) -> -1).
If find should not guarantee that the value returned is a valid index(when start isn't a valid index), then the first sentence should be rephrased to avoid giving this idea(and the comparisons in stringlib/find.h should be swapped to have the correct behaviour).
For example, maybe, it could be "Return the lowest index where substring sub is found (in S?), such that sub is contained in S[start:end]. ...

Back to comp.lang.python | Previous | NextPrevious 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