Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.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; 'bug.': 0.07; 'correct.': 0.07; 'indices': 0.07; 'interpreted': 0.07; 'raises': 0.07; 'valueerror:': 0.07; 'cc:addr:python-list': 0.09; '0),': 0.09; 'lines:': 0.09; 'slices': 0.09; 'subject:()': 0.09; 'typeerror:': 0.09; 'exception': 0.13; 'def': 0.13; '"using': 0.16; 'boundary,': 0.16; 'boundary.': 0.16; 'dictionaries': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'index.': 0.16; 'integers,': 0.16; 'sentinel': 0.16; 'subject:versus': 0.16; 'wait.': 0.16; 'wrote:': 0.16; 'string': 0.17; 'try:': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '"",': 0.22; 'either.': 0.22; 'rid': 0.22; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; "doesn't": 0.26; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; '13,': 0.29; 'index,': 0.29; 'anyone': 0.32; 'possibly': 0.32; "who's": 0.32; "d'aprano": 0.33; 'right?': 0.33; 'steven': 0.33; 'traceback': 0.33; 'equal': 0.34; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'returning': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'too': 0.36; 'there': 0.36; 'beginning': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'version': 0.38; 'anything': 0.38; 'someone': 0.38; 'along': 0.39; 'rather': 0.39; 'skip:n 10': 0.62; 'more': 0.63; '>>>>>': 0.66; 'jul': 0.72; 'chrisa': 0.84; "it'd": 0.84; 'one).': 0.84; 'proves': 0.84; 'subscripts.': 0.84; 'to:none': 0.91; 'maybe,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=VzzPMLNrGEqsjWDVBXOj0OE0XJ0WMSStgo8vNVDVD4k=; b=PZTfQqqDLyA+JHz3qr1xDCVJwP1AjbaM6oHSktQrT+BH2ySMNbNmu6qRbx5wvwbvob biDBSHoVVUAMkpr123peJBzEQoJUSJeAJ/FzjKr99Fb+P1xhBz+n3g1IFbBxefidZqQ9 zMWwD2kNge/yQTr8QT1zzFXuhnThie8eYMG17eaFMoBKAhm3oKY5RERIan8gnwTscaAM AOZe1nhE9yBmAJoHCCLazNhceWhv4Nd65KBNtQyWR8acjKM9fUDQf/4R5tixvVqQGnKp fMK4TiavlOxC6ayLxxPywRL4F1U0s/rYA1wg78jDPLh3AmyDO9083DHWPvmJAqHzrMGY O1Cw== MIME-Version: 1.0 X-Received: by 10.107.165.142 with SMTP id o136mr24786659ioe.120.1436850821521; Mon, 13 Jul 2015 22:13:41 -0700 (PDT) In-Reply-To: <55a496f6$0$1580$c3e8da3$5496439d@news.astraweb.com> References: <55a480c0$0$1673$c3e8da3$5496439d@news.astraweb.com> <55a496f6$0$1580$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 14 Jul 2015 15:13:41 +1000 Subject: Re: str.index() and str.find() versus only list.index() From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436850824 news.xs4all.nl 2889 [2001:888:2000:d::a6]:37761 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93779 On Tue, Jul 14, 2015 at 2:58 PM, Steven D'Aprano wrote: > On Tuesday 14 July 2015 14:07, Ian Kelly wrote: > >> On Mon, Jul 13, 2015 at 9:23 PM, Steven D'Aprano >> wrote: >>> Correct. But rather than removing it, it would be better to take a leaf >>> out of re.match's book and return None as the sentinel. That would >>> eliminate the "using -1 as a valid index" bug. >> >> I disagree on both counts. >> >>>>> s = 'abc' >>>>> s[None:None] >> 'abc' > > > Well wadda ya know, I just learned something new. I was thinking more along > these lines: > > py> 'abc'[None] > Traceback (most recent call last): > File "", line 1, in > TypeError: string indices must be integers, not NoneType Which proves that None is not a valid index, but it is a valid slice boundary. Given that the return value will often be used for slicing as well as indexing, it'd be good to have something that's not valid for either. How about -1.0? It's equal to -1 in case someone uses (str.find(...) == -1), it's less than zero in case they do (str.find(...) < 0), and it's invalid in both slices and string subscripts. There's no way that can break anything, right? ... oh wait. XKCD 1172. And anyone who's adding 1 to the position and using that as a slice boundary, which will smoothly and without error work with the beginning of the string if something isn't found (eg s[s.find("-"):] will be everything after the first hyphen, or the whole string if there isn't one). >> Better IMO to just have the one non-redundant method that raises an >> exception rather than returning anything that could possibly be >> interpreted as a string index. > > > Well, maybe, but if you got rid of str.find, the first thing people would do > is recreate it: > > def find(*args): > try: > return str.index(*args) > except ValueError: > return -1 > > > Having a version of str.index that returns a sentinel is just too damn > handy. Same as dictionaries have [] and .get(), although find doesn't allow you to change the sentinel. ChrisA