Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #62555
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'true,': 0.05; 'returned.': 0.07; 'subject:code': 0.07; '"c"': 0.09; 'executed': 0.09; 'false.': 0.09; 'lawrence': 0.09; 'means,': 0.09; 'operand': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'thread': 0.14; '"a"': 0.16; '"b"': 0.16; '"or"': 0.16; 'a()': 0.16; 'a():': 0.16; 'b()': 0.16; 'b():': 0.16; 'be:': 0.16; 'c():': 0.16; 'cleaner': 0.16; 'cui': 0.16; 'executed.': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'pythonic': 0.16; 'really?': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'roy': 0.16; 'simpson': 0.16; 'subject:python': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'cheers,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'equivalent': 0.26; 'second': 0.26; 'header:In- Reply-To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'code': 0.31; 'bunch': 0.31; 'exceptions': 0.31; 'something': 0.35; 'requirement': 0.35; 'there': 0.35; 'false': 0.36; 'i.e.': 0.36; 'otherwise.': 0.36; 'received:com.au': 0.36; 'returning': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'being': 0.38; 'received:211': 0.38; 'sure': 0.39; 'chain': 0.60; 'first': 0.61; 'content-disposition:inline': 0.62; 'more': 0.64; 'believe': 0.68; 'frank': 0.68; 'smith': 0.68; 'evaluate': 0.72; 'article': 0.77; 'do:': 0.91 |
| Date | Mon, 23 Dec 2013 08:39:46 +1100 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | Roy Smith <roy@panix.com> |
| Subject | Re: cascading python executions only if return code is 0 |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Disposition | inline |
| In-Reply-To | <roy-A2E16D.15053422122013@news.panix.com> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| References | <roy-A2E16D.15053422122013@news.panix.com> |
| X-Optus-CM-Score | 0 |
| X-Optus-CM-Analysis | v=2.1 cv=HZAtEE08 c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=r_9ksr2YOKQA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=jUhKah_ObyYA:10 a=VUfPOBp7AAAA:8 a=8AHkEIZyAAAA:8 a=BrDiTsk0AAAA:8 a=UqCG9HQmAAAA:8 a=7UENScbj9QBbnpn4tUcA:9 a=WDGaXeABQa5w-0fn:21 a=oQLYR-xVGUDNlnhD:21 a=CjuIK1q_8ugA:10 a=5hK03km2n30A:10 a=VhVvL8HfBcoA:10 a=-hJg1tCh9CgA:10 a=4JFMnUiinsEA:10 |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.4512.1387748397.18130.python-list@python.org> (permalink) |
| Lines | 68 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1387748397 news.xs4all.nl 2896 [2001:888:2000:d::a6]:52769 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:62555 |
Show key headers only | View raw
On 22Dec2013 15:05, Roy Smith <roy@panix.com> wrote: > In article <mailman.4504.1387740695.18130.python-list@python.org>, > Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > > > On 22/12/2013 19:17, Roy Smith wrote: > > > In article <mailman.4500.1387739297.18130.python-list@python.org>, > > > Frank Cui <ycui@outlook.com> wrote: > > >> 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() > > > > > > 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? :) > > I believe what Mark is so elegantly trying to say is, "Roy is a dufus > and got that backwards". You need to return something which is false to > make the next one in the chain get executed. > > $ cat or.py > def a(): > print "a" > return 0 > > def b(): > print "b" > return 1 > > def c(): > print "c" > return 0 > > a() or b() or c() > > > $ python or.py > a > b Yeah. Roy's code _depends_ upon the return value being equivalent to False. A better approach would be: a() == 0 and b() == 1 and c() == 0 i.e. to explicitly check each return code against the expected/desired value instead of relying on some flukey coincidental property of the (arbitrary) numeric value returned. Better still is the suggestion elsewhere in the thread to make the functions raise exceptions on error instead of returning a number. Cheers, -- Cameron Simpson <cs@zip.com.au>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
cascading python executions only if return code is 0 Frank Cui <ycui@outlook.com> - 2013-12-22 15:37 -0300
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-22 14:17 -0500
Re: cascading python executions only if return code is 0 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-22 19:31 +0000
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-22 15:05 -0500
Re: cascading python executions only if return code is 0 Cameron Simpson <cs@zip.com.au> - 2013-12-23 08:39 +1100
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-22 16:53 -0500
Re: cascading python executions only if return code is 0 Cameron Simpson <cs@zip.com.au> - 2013-12-23 09:11 +1100
Re: cascading python executions only if return code is 0 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-22 22:10 -0500
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-23 14:22 +1100
Re: cascading python executions only if return code is 0 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-23 03:43 +0000
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-23 14:45 +1100
Re: cascading python executions only if return code is 0 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-23 03:54 +0000
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-23 14:59 +1100
RE: cascading python executions only if return code is 0 Frank Cui <ycui@outlook.com> - 2013-12-22 16:10 -0300
Re: cascading python executions only if return code is 0 Ned Batchelder <ned@nedbatchelder.com> - 2013-12-22 14:49 -0500
Re: cascading python executions only if return code is 0 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-23 00:08 +0000
RE: cascading python executions only if return code is 0 Frank Cui <ycui@outlook.com> - 2013-12-22 16:35 -0300
Re: cascading python executions only if return code is 0 Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-23 13:30 +1300
Re: cascading python executions only if return code is 0 Rick Johnson <rantingrickjohnson@gmail.com> - 2013-12-22 14:27 -0800
RE: cascading python executions only if return code is 0 Frank Cui <ycui@outlook.com> - 2013-12-22 19:14 -0300
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-23 09:51 +1100
Re: cascading python executions only if return code is 0 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-22 23:02 +0000
Re: cascading python executions only if return code is 0 Rick Johnson <rantingrickjohnson@gmail.com> - 2013-12-22 15:57 -0800
Re: cascading python executions only if return code is 0 Ned Batchelder <ned@nedbatchelder.com> - 2013-12-23 07:27 -0500
Re: cascading python executions only if return code is 0 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-23 12:44 +0000
Re: cascading python executions only if return code is 0 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-23 00:25 +0000
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-22 20:24 -0500
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-23 12:35 +1100
Re: cascading python executions only if return code is 0 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-23 13:33 +1100
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-23 14:09 +1100
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-22 23:57 -0500
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-23 16:09 +1100
Re: cascading python executions only if return code is 0 Ethan Furman <ethan@stoneleaf.us> - 2013-12-23 04:45 -0800
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-23 10:10 -0500
Re: cascading python executions only if return code is 0 Ethan Furman <ethan@stoneleaf.us> - 2013-12-23 08:00 -0800
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-26 20:37 -0500
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-27 12:44 +1100
Re: cascading python executions only if return code is 0 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-26 21:20 -0500
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-27 13:27 +1100
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-26 23:29 -0500
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-27 15:40 +1100
Re: cascading python executions only if return code is 0 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-27 11:15 -0500
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-27 11:42 -0500
Re: cascading python executions only if return code is 0 Rustom Mody <rustompmody@gmail.com> - 2013-12-27 22:39 +0530
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-28 08:24 +1100
Re: cascading python executions only if return code is 0 pecore@pascolo.net - 2013-12-28 00:59 +0100
Re: cascading python executions only if return code is 0 Gene Heskett <gheskett@wdtv.com> - 2013-12-27 19:12 -0500
Re: cascading python executions only if return code is 0 Tim Chase <python.list@tim.thechases.com> - 2013-12-26 20:50 -0600
Re: cascading python executions only if return code is 0 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-27 15:39 +1100
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-27 15:45 +1100
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-27 00:05 -0500
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-27 16:15 +1100
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-27 00:41 -0500
Re: cascading python executions only if return code is 0 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-27 18:00 +0000
Re: cascading python executions only if return code is 0 Grant Edwards <invalid@invalid.invalid> - 2013-12-30 17:36 +0000
Re: cascading python executions only if return code is 0 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-24 04:05 +1100
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-23 11:03 -0800
Re: cascading python executions only if return code is 0 Chris Angelico <rosuav@gmail.com> - 2013-12-24 06:12 +1100
RE: cascading python executions only if return code is 0 Nick Cash <nick.cash@npcinternational.com> - 2013-12-23 19:31 +0000
Re: cascading python executions only if return code is 0 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-24 14:41 +1100
Re: cascading python executions only if return code is 0 Roy Smith <roy@panix.com> - 2013-12-23 22:58 -0500
Re: cascading python executions only if return code is 0 Peter Otten <__peter__@web.de> - 2013-12-23 18:45 +0100
Re: cascading python executions only if return code is 0 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-23 13:20 +0000
Re: cascading python executions only if return code is 0 Ethan Furman <ethan@stoneleaf.us> - 2013-12-23 04:38 -0800
Re: cascading python executions only if return code is 0 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-23 10:12 -0500
Re: cascading python executions only if return code is 0 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-24 04:13 +1100
Re: cascading python executions only if return code is 0 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-23 15:22 +0000
csiph-web