Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91840 > unrolled thread
| Started by | acdr <mail.acdr@gmail.com> |
|---|---|
| First post | 2015-06-02 16:00 +0200 |
| Last post | 2015-06-02 16:00 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: for...else acdr <mail.acdr@gmail.com> - 2015-06-02 16:00 +0200
| From | acdr <mail.acdr@gmail.com> |
|---|---|
| Date | 2015-06-02 16:00 +0200 |
| Subject | Re: for...else |
| Message-ID | <mailman.60.1433253614.13271.python-list@python.org> |
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 <jeanmichel@sequans.com> wrote: > ----- 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 top | Article view | comp.lang.python
csiph-web