Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'indicating': 0.07; 'returned.': 0.07; 'subject:code': 0.07; 'assuming': 0.09; 'desired,': 0.09; 'false.': 0.09; 'indicates': 0.09; 'run,': 0.09; 'cc:addr:python-list': 0.11; 'thread': 0.14; '"or"': 0.16; 'a()': 0.16; 'arbitrary.': 0.16; 'b()': 0.16; 'be:': 0.16; 'boolean': 0.16; 'chained': 0.16; 'count.': 0.16; 'dislike': 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; 'op.': 0.16; 'possibly,': 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; 'exception': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'seems': 0.21; 'command': 0.22; 'example': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'parse': 0.24; 'tend': 0.24; 'cheers,': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'equivalent': 0.26; 'suggested': 0.26; 'values': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'feature': 0.29; 'raise': 0.29; 'errors': 0.30; 'returned': 0.30; "i'm": 0.30; 'went': 0.31; 'code': 0.31; 'exceptions': 0.31; 'yes.': 0.31; 'file': 0.32; 'option': 0.32; 'run': 0.32; 'another': 0.32; 'agree': 0.35; 'something': 0.35; 'case,': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'his/her': 0.36; 'i.e.': 0.36; 'received:com.au': 0.36; 'returning': 0.36; 'charset:us- ascii': 0.36; "i'll": 0.36; 'possible': 0.36; 'two': 0.37; 'list': 0.37; 'being': 0.38; 'system,': 0.38; 'convention': 0.38; 'received:211': 0.38; 'that,': 0.38; 'does': 0.39; 'bad': 0.39; 'success.': 0.39; 'sure': 0.39; 'skip:u 10': 0.60; 'success': 0.61; "you're": 0.61; 'first': 0.61; 'here:': 0.62; 'times': 0.62; 'content-disposition:inline': 0.62; 'myself': 0.63; 'more': 0.64; 'different': 0.65; 'finally': 0.65; 'design.': 0.68; 'smith': 0.68; 'article': 0.77; 'william': 0.81; 'failure:': 0.84; 'intends': 0.84; 'succeed.': 0.84; 'succession': 0.84; 'unclear': 0.84 Date: Mon, 23 Dec 2013 09:11:25 +1100 From: Cameron Simpson To: Roy Smith 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: User-Agent: Mutt/1.5.21 (2010-09-15) References: X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=bpB1Wiqi 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=ofmUr7BiZHU-DgM89QgA:9 a=tjod9dOZOdeyW8mX:21 a=7zGoZpcFmT69GSfB:21 a=CjuIK1q_8ugA:10 a=5hK03km2n30A:10 a=VhVvL8HfBcoA: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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387750297 news.xs4all.nl 2850 [2001:888:2000:d::a6]:58211 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62557 On 22Dec2013 16:53, Roy Smith wrote: > In article , > Cameron Simpson wrote: > > Roy's code _depends_ upon the return value being equivalent to False. > > Yes. You view this as a flaw. I view it as a feature :-) When I write functions which return a boolean indicating success/failure, I try to make that boolean be "true" for success. Now, I do take the point that these functions seem to take the unix-exit-code convention that zero is success (leaving the many values of "non-zero" to indicate flavours of failure as desired, as we have many types of exceptions). Or, possibly, that a non-zero return indicates the number of errors encountered; I do that myself for things like option or file parsing, where I explicitly want to parse of much of a command line (or whatever) as possible before rejecting things; few things annoy me as much as a command that barfs about the first usage error and aborts instead of barfing multiple times and aborting. Unless it is a command that does the same and then fails to recite a usage message after the barf. (Yes, almost every GNU command on the planet: I'm looking at you!) However, in this count-of-errors scenario I tend to try to return a list of errors, not a count. But regardless, I consider code that goes: a() or b() or c() as a test for _success_ of a(), b() and c() in succession to be misleading at best. When I write that above incantation it is a test for failure: try this, or try that, or finally try this other thing. > > 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. > > You're assuming it's arbitrary. I'm saying do it that way by design. The counter example the above is based upon deliberately returned 1 for success from b(), IIRC. Different design. The OP was unclear about his/her design rationale. > > Better still is the suggestion elsewhere in the thread to make the functions > > raise exceptions on error instead of returning a number. > > Possibly. But, I think of exceptions as indicating that something went > wrong. I think of failure as "something went wrong". Yes, I'll grant there are shades of intent here. > There's two possible things the OP was trying to do here: > > 1) He intends that all of the functions get run, but each one can only > get run if all the ones before it succeed. In that case, I agree that > the exception pattern makes sense. His cascading if-statement in the OP suggested this to me. > 2) He intends that each of the functions gets tried, and the first one > that can return a value wins. If that's the case, the "or" chaining > seems more natural. I'm pretty sure that wasn't his intent, again based on my recollection of the OP. But I still dislike "a() or b() or c()" as a test for chained success; I think it is a bad idiom. Cheers, -- Cameron Simpson I must construct my own System, or be enslaved to another Man's. - William Blake