Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79098
| 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 | <rosuav@gmail.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; '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 | <CAEhE+OknxQs0XdENq_MR=HyER1qz5_JXXRHxO35RYV3h-K13KQ@mail.gmail.com> |
| References | <CAEhE+OknxQs0XdENq_MR=HyER1qz5_JXXRHxO35RYV3h-K13KQ@mail.gmail.com> |
| Date | Wed, 15 Oct 2014 01:22:03 +1100 |
| Subject | Re: Short syntax for try/pass |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <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 <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14850.1413296526.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Wed, Oct 15, 2014 at 1:08 AM, Leonardo Giordani
<giordani.leonardo@gmail.com> 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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Short syntax for try/pass Chris Angelico <rosuav@gmail.com> - 2014-10-15 01:22 +1100
csiph-web