cascading python executions only if return code is 0

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Dec 22 20:31:21 CET 2013


On 22/12/2013 19:17, Roy Smith wrote:
> In article <mailman.4500.1387739297.18130.python-list at python.org>,
>   Frank Cui <ycui at outlook.com> wrote:
>
>> hey guys,
>> I have a requirement where I need to sequentially execute a bunch of
>> executions, each execution has a return code. the followed executions should
>> only be executed if the return code is 0. is there a cleaner or more pythonic
>> way to do this other than the following ?
>> if a() == 0:    if b() == 0:        c()
>> Thanks for your input.
>> frank 		 	   		
>
> Yup!  Just do:
>
> a() or b() or c()
>
> The "or" operation has what's known as "short-circuit" semantics.  That
> means, if the first operand is true, it doesn't evaluate the second
> operand.  Just make sure that a(), b(), and c() all return something
> which is true if they succeed and false otherwise.
>

Really? :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list