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


Groups > comp.lang.python > #91837

Re: for...else

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=588e0e812=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.167
X-Spam-Level *
X-Spam-Evidence '*H*': 0.67; '*S*': 0.00; 'cleanup': 0.07; 'cleanup,': 0.09; 'def': 0.14; 'blame': 0.16; 'contextlib': 0.16; 'try:': 0.22; 'pass': 0.22; 'to:2**1': 0.22; '2015': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'raise': 0.24; 'example': 0.25; 'yield': 0.27; '-----': 0.29; 'themselves': 0.29; 'function': 0.30; 'becomes': 0.31; 'print': 0.31; 'class': 0.33; 'sent:': 0.35; 'to:addr:python-list': 0.35; 'cc:': 0.35; 'really': 0.35; 'handle': 0.36; 'but': 0.36; 'except': 0.36; 'subject:': 0.36; 'email addr:python.org': 0.37; 'subject:: ': 0.37; 'thank': 0.39; 'to:addr:python.org': 0.39; 'even': 0.61; 'received:194': 0.61; 'information': 0.62; 'you.': 0.64; 'here': 0.66; 'better.': 0.66; 'person,': 0.66; 'email name :python-list': 0.67; 'privileged.': 0.67; 'notice:': 0.67; 'disclose': 0.71; 'calculations': 0.84; 'june,': 0.84; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="5.13,540,1427752800"; d="scan'208";a="4413197"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Tue, 2 Jun 2015 15:46:55 +0200 (CEST)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To acdr <mail.acdr@gmail.com>, python-list@python.org
In-Reply-To <CANFcO8sQ0BjHU+7i6XEpaygZLhhi2R0zh0enNk4zk=L8QT3=kg@mail.gmail.com>
Subject Re: for...else
MIME-Version 1.0
X-Mailer Zimbra 7.2.7_GA_2942 (ZimbraWebClient - GC37 (Linux)/7.2.7_GA_2942)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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.58.1433252818.13271.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433252818 news.xs4all.nl 2960 [2001:888:2000:d::a6]:33946
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91837

Show key headers only | View raw


----- Original Message -----
> From: "acdr" <mail.acdr@gmail.com>
> To: "Jean-Michel Pichavant" <jeanmichel@sequans.com>
> Cc: python-list@python.org
> Sent: Tuesday, 2 June, 2015 2:52:21 PM
> Subject: Re: for...else
> 
> That would work for my example, but it would only really work if all
> the calculations are in a nice function. 

You cannot blame me for considering the example you provided ;)

What about this:

class Cleanup(Exception): pass

try:
  for x in it:
    if c1():
      raise Cleanup()
    c2()
    if c3():
      raise Cleanup()
except Cleanup:
  #do the cleanup
   pass

If you can make c1 c3 raise themselves Cleanup, it's even better.

Now if you're able to write a cleanup function that can handle the case when there's nothing to clean, everything becomes crystal clear:

from contextlib import contextmanager

@contextmanager
def cleanup():
  yield
  # here do the cleaning
  print 'I am cleaning'

def do_the_job():
  if c1() : return
  c2()
  if c3() : return

with cleanup():
  do_the_job()

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: for...else Jean-Michel Pichavant <jeanmichel@sequans.com> - 2015-06-02 15:46 +0200

csiph-web