Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'lines,': 0.07; '"if': 0.09; 'agree,': 0.09; 'suppress': 0.09; 'try:': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; '1:08': 0.16; 'better:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'indexerror:': 0.16; 'subject:pass': 0.16; 'wrote:': 0.18; 'wed,': 0.18; "python's": 0.19; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; '15,': 0.26; 'pass': 0.26; 'least': 0.26; 'subject:/': 0.26; 'code:': 0.26; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'converting': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'style': 0.33; 'could': 0.34; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'possible': 0.36; 'version,': 0.38; 'times': 0.62; 'subject:try': 0.84; '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=Qt6KSgJpwemZ35MYPTTRpgUe8gMwgCnmNJO7VUem3Ok=; b=bG2P5tIp3Cfx640DU3WauRuT2LgD2vJc1iM2mP4/NZ7dmrV6J7cS46i+03fEtlirH6 NvV0aGJ5rF+qprzPEZwUrBdN/b1C541/97glbUmUQ8ZwZiYJUsImMkmpZW4OwyqKPeqk IwKcWiAkQoyqMkJvLhUwgT6es+CEphQuEYx56bX0EhXI1IznI4G38mQOIXrL+9lyXfTu 5F4Hn409Hc2o+QjeNLJ/DV70iEoI7ZF/jvF/TdwJUpxQgfsDHzswGBuQHhUSHrD1z+Xg IVC1qND94ezS8bB/xYnx4Ph+Ft6I+t3eXRELuGXwEZJMquLhTb7vioeauvloKK1bXG5K OMtg== MIME-Version: 1.0 X-Received: by 10.42.110.195 with SMTP id r3mr5440990icp.12.1413296523458; Tue, 14 Oct 2014 07:22:03 -0700 (PDT) In-Reply-To: References: Date: Wed, 15 Oct 2014 01:22:03 +1100 Subject: Re: Short syntax for try/pass 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413296526 news.xs4all.nl 2851 [2001:888:2000:d::a6]:43169 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79098 On Wed, Oct 15, 2014 at 1:08 AM, Leonardo Giordani wrote: > a lot of times the following pattern pops out in Python code: > > try: > somecode > except SomeException: > pass > > Converting the code to a non-EAFP version, for example > > if len(lst) != 0: > lst[0] = lst[0] + 1 This could be just "if lst:", but I agree, LBYL is not Python's style (and isn't always possible anyway). You can at least squish it up onto less lines, which might look better: try: lst[0] += 1 except IndexError: pass Alternatively, you can use this style: from contextlib import suppress with suppress(IndexError): lst[0] += 1 ChrisA