Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.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.043 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'cleanup': 0.07; 'rename': 0.07; 'cleanup,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.14; '"break"': 0.16; 'blame': 0.16; 'contextlib': 0.16; 'effect,': 0.16; 'guessing': 0.16; 'manner,': 0.16; 'verbose': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'try:': 0.22; 'pass': 0.22; 'cc:no real name:2**0': 0.23; '2015': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'raise': 0.24; 'example': 0.25; 'skip:" 20': 0.26; 'yield': 0.27; 'message-id:@mail.gmail.com': 0.28; "i'm": 0.29; '-----': 0.29; 'themselves': 0.29; 'function': 0.30; 'becomes': 0.31; 'print': 0.31; 'structure': 0.32; 'class': 0.33; 'received:google.com': 0.34; 'sent:': 0.35; 'cc:': 0.35; 'really': 0.35; 'handle': 0.36; 'but': 0.36; 'except': 0.36; 'there': 0.36; 'subject:': 0.36; 'email addr:python.org': 0.37; 'subject:: ': 0.37; 'suggestion': 0.37; '(with': 0.38; 'thank': 0.39; 'your': 0.60; 'even': 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; 'clarity.': 0.84; "it'd": 0.84; 'june,': 0.84; 'medium.': 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:to :cc:content-type; bh=eOIBwxsl35djHQ0xByHo53LD0Thbl6qIb0EPit9X0h8=; b=qasM4lK25hDLIyRWWXtueYGqcGmE6GSya4avqZAl5FgY/3RgLyvjZPpSlTyYOZ+CfZ srt63chiua4qpRW+auMObndR43bMFDfYiIbifCsoJSMnxzoWTzFmrlCtMQFpcoCEAvNl H5H7xbZV7tHsF+BNw0WnwTPmzNzUELufTLCOsxjwxNZLCOwoS0nyQxAOYUhhhX4kggIs BpBh1uGFshl40sr3fCpiNTvRH1BxHGk28pujZMElwnsUCtZgmPLTWOMasmUGW65r0VJv Wn1GsjhhkAreT9DP8rsRN+f/5bSQvoGT4nnJMEE68NaYAzdxwHdxo97rFMrBLeSNjUPU GjyA== MIME-Version: 1.0 X-Received: by 10.194.249.196 with SMTP id yw4mr50347842wjc.144.1433253612453; Tue, 02 Jun 2015 07:00:12 -0700 (PDT) In-Reply-To: <23156522.1965199.1433252815342.JavaMail.root@sequans.com> References: <23156522.1965199.1433252815342.JavaMail.root@sequans.com> Date: Tue, 2 Jun 2015 16:00:12 +0200 Subject: Re: for...else From: acdr To: Jean-Michel Pichavant 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433253614 news.xs4all.nl 2867 [2001:888:2000:d::a6]:43715 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91840 The first solution in your e-mail (with a Cleanup exception) is definitely very close, functionally, to what I want to accomplish. In effect, it's the same structure as my original suggestion of "for...then...else", except now it'd be "try: for...else...except". That's workable. I can even cheat and rename the Cleanup class to "Break" for clarity. I'm guessing that there is not much support for functionality like this to be built in, in a much less verbose manner, like the "for...else" functionality? :P On 6/2/15, Jean-Michel Pichavant wrote: > ----- Original Message ----- >> From: "acdr" >> To: "Jean-Michel Pichavant" >> 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. >