Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'string.': 0.05; 'indexing': 0.07; 'string': 0.09; 'lawrence': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'jan': 0.12; '(also': 0.16; 'bit.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'point!': 0.16; 'slice,': 0.16; 'subject:bit': 0.16; 'tuples,': 0.16; 'type),': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'byte': 0.24; 'certainly': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'code': 0.31; '(since': 0.31; 'index,': 0.31; 'probably': 0.32; 'run': 0.32; 'checking': 0.33; 'fri,': 0.33; 'not.': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; '14,': 0.36; 'too': 0.37; 'being': 0.38; 'problems': 0.38; 'rather': 0.38; 'hope': 0.61; "you're": 0.61; 'back': 0.62; 'more': 0.64; 'worth': 0.66; 'special': 0.74; '2014,': 0.84; '2013,': 0.91; 'to:none': 0.92 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=RGe6yaakObq0FDuex71MPyo+wuX2dAirkNfeIq3e1ug=; b=QLQuTlmoQnCG0M39C7FpYxDRxZR8Jsb1PcjV+eUfDTJ4MyFiecdToL1Ir4oUeO086n Afe1C2ZyzwHwDCXIjQHLoHLk09B7SEVsienGifkTMpj4m6V+9VkUF5+mGZUlLLU+gMcv DAidmj9498uxFspYmL5Wzf1RoNYquaY/bNIIbkaQ/acwlN303Z5gjq7VSAQNYRbSQ0Yw mVnVtey9M0NixI0/Vnvd33GvqIFnX9DnnbxPY6QH2leRaniLYKFrafjDfDDv0n8aiF7R iwJIFCR8zH3RejyNkF4PekIVtRpUPTezVt2O54F+qBZ4BsQH+pm00km77KUqebOsjXe5 /ibQ== MIME-Version: 1.0 X-Received: by 10.68.98.3 with SMTP id ee3mr4047327pbb.31.1392323380340; Thu, 13 Feb 2014 12:29:40 -0800 (PST) In-Reply-To: References: <4cc09129-43ee-4205-a24c-03f92b594abc@googlegroups.com> <52FD1B01.80200@stoneleaf.us> Date: Fri, 14 Feb 2014 07:29:40 +1100 Subject: Re: A curious bit of code... 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.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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392323389 news.xs4all.nl 2871 [2001:888:2000:d::a6]:35946 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66232 On Fri, Feb 14, 2014 at 6:32 AM, Mark Lawrence wrote: >> There will be an exception only if it is zero-length. But good >> point! That's a pretty sneaky way to avoid checking for a >> zero-length string. Is it a popular idiom? >> > > I hope not. The use of slicing rather than indexing to avoid problems when the string's too short? I don't know about popular, but I've certainly used it a good bit. For the specific case of string comparisons you can use startswith/endswith, but slicing works with other types as well. Also worth noting: Python 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> s1,s2=b"asdf",u"asdf" >>> s1[:1],s2[:1] ('a', u'a') >>> s1[0],s2[0] ('a', u'a') Python 3.4.0b2 (v3.4.0b2:ba32913eb13e, Jan 5 2014, 16:23:43) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> s1,s2=b"asdf",u"asdf" >>> s1[:1],s2[:1] (b'a', 'a') >>> s1[0],s2[0] (97, 'a') When you slice, you get back the same type as you started with. (Also true of lists, tuples, and probably everything else that can be sliced.) When you index, you might not; strings are a special case (since Python lacks a "character" type), and if your code has to run on Py2 and Py3, byte strings stop being that special case in Py3. So if you're working with a byte string, it might be worth slicing rather than indexing. (Though you can still use startswith/endswith, if they suit your purpose.) ChrisA