for...else

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Jun 2 08:01:03 EDT 2015


----- Original Message -----
> From: "acdr" <mail.acdr at gmail.com>
> To: python-list at python.org
> Sent: Tuesday, 2 June, 2015 1:26:42 PM
> Subject: for...else
> 
> Hi,
> 
> Currently, in various places in my code, I have the equivalent of:
> 
> for x in it:
>     if complicated_calculation_1():
>         cleanup()
>         break
>     complicated_calculation_2()
>     if complicated_calculation_3():
>         cleanup()
>         break

Hi

With the following layout, adding calculation steps is just a matter of adding a line

for x ion it:
  for calc, cbk in [
    (complicated_calculation_1, cleanup),
    (complicated_calculation_2, None),
    (complicated_calculation_3, cleanup),
  ]:
    if calc() and cbk:
      cbk()
      break
      
It might give you ideas.

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.


More information about the Python-list mailing list