Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.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; 'scripts': 0.03; 'operator': 0.03; 'that?': 0.05; 'subject:Python': 0.06; 'context': 0.07; 'plenty': 0.07; 'purpose.': 0.07; '"if': 0.09; '"or': 0.09; 'apis': 0.09; 'bash': 0.09; 'contexts': 0.09; 'logic': 0.09; 'parameter': 0.09; 'pretend': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; '"and"': 0.16; '"or"': 0.16; 'boolean': 0.16; 'both,': 0.16; 'emit': 0.16; 'finney': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nonzero': 0.16; 'sense,': 0.16; 'sense:': 0.16; 'sudo': 0.16; 'language': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'thu,': 0.19; 'value.': 0.19; 'command': 0.22; 'feb': 0.22; 'machine': 0.22; 'code,': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'install': 0.23; 'error': 0.23; 'alternate': 0.24; 'case.': 0.24; "shouldn't": 0.24; 'fairly': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'source': 0.25; 'right.': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'specifically': 0.29; 'chris': 0.29; 'am,': 0.29; 'unix': 0.29; 'message-id:@mail.gmail.com': 0.30; 'asked': 0.31; 'code': 0.31; 'url:wiki': 0.31; '"do': 0.31; 'bad.': 0.31; 'though.': 0.31; 'url:wikipedia': 0.31; 'writes:': 0.31; 'file': 0.32; 'sense': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'doubt': 0.36; 'programming,': 0.36; "didn't": 0.36; 'similar': 0.36; 'url:org': 0.36; 'ben': 0.38; 'convention': 0.38; 'massive': 0.38; 'rather': 0.38; 'that,': 0.38; 'either': 0.39; 'commands': 0.60; 'tell': 0.60; 'simple': 0.61; 'making': 0.63; 'name': 0.63; 'kind': 0.63; 'more': 0.64; 'talking': 0.65; '26,': 0.68; 'reads': 0.68; 'url:%1': 0.72; 'other.': 0.75; '2015': 0.84; 'answer:': 0.84; 'beats': 0.84; 'borrow': 0.84; 'fails,': 0.84; 'subject:Practices': 0.84; 'destination': 0.91; 'notion': 0.91; 'response,': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=Buyf93My6rzGQ1xxrrGHRssFmRvmnUlLUvFCHEskAus=; b=l1d3xDbyO17nRy43PnU2loiGWR23QVQTM+GB78pU1KQzKpNW8leq1i2bcJR6XkN3h7 ppmkqmmcXgOmSpyieBXZwyAtQBATJ21Ybi51NMxiRb1BJAgh9S5hWoKT0kf1b8LpOO6p F2tyn38bO4/d3/E4RlkMP4+3bZ3mzusmblbSL0WdVu8GzXbF/TFag+j/KSOcwJSnkuYF l9tMd5Xe6k3hrBZNbHVyr+5EX+sQvC6F0ycgA8MBSsVoFjX8yYYpgprsD4VMU/aSiP1e rVfOElCjlMBEPEpgirjBT/X84GoOSSU26hbqhHpEYFb1RMrftVYWrZDCbVJtNHjzCu+B etvQ== MIME-Version: 1.0 X-Received: by 10.107.160.212 with SMTP id j203mr8395408ioe.43.1424911051379; Wed, 25 Feb 2015 16:37:31 -0800 (PST) In-Reply-To: <85zj814jmb.fsf@benfinney.id.au> References: <85bnkh5z96.fsf@benfinney.id.au> <85zj814jmb.fsf@benfinney.id.au> Date: Thu, 26 Feb 2015 11:37:31 +1100 Subject: Re: Python Worst Practices From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424911053 news.xs4all.nl 2859 [2001:888:2000:d::a6]:48936 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86455 On Thu, Feb 26, 2015 at 11:11 AM, Ben Finney w= rote: > Chris Angelico writes: > >> On Thu, Feb 26, 2015 at 10:48 AM, Ben Finney wrote: >> > Chris Angelico writes: >> > >> >> (Flipping the booleans makes no sense to me. When would 0 mean true >> >> and 1 mean false? [=E2=80=A6]) >> > >> > The Unix commands =E2=80=98true=E2=80=99 and =E2=80=98false=E2=80=99 f= ollow that convention >> > . >> >> Yes, but my point is: You shouldn't need to rebind those names (or >> have names "true" and "false" for 0 and 1). > > That's not what you asked, though. You asked =E2=80=9CWhen would 0 mean t= rue and > 1 mean false?=E2=80=9D My answer: in all Unix shell contexts. My bad. I was talking in a context of Python programming, specifically with APIs where you would use some kind of true/false flag as either a function parameter or a return value. There are plenty of contexts where you want a success/failure response, but you can name your constants "success" and "failure" for that, rather than "true" and "false". >> Instead, use "success" and "failure". > > You'd better borrow the time machine and tell the creators of Unix. The > meme is already established for decades now. IMO the whole system of boolean logic in shell scripts is a massive pile of hacks. In its purest sense, you have "do this, and if it succeeds, do that": cp source destination && rm source which makes reasonable sense with an "and" operator - do this, and do this - and the converse makes similar sense with an "or" operator: cp source destination || echo Oopsie On the assumption that a nonzero return code from cp means it didn't copy the file, this reads as "copy the file or emit that message", which is exactly right. Nice. But that's stretching the notion of "and" and "or" a little, to the point where booleans and comparisons stop making so much sense: [ -f /usr/local/bin/python ] || sudo make install Is that a success/failure, or is it an if-then? Kinda neither, kinda both, kinda one built on the other built on the other. cp source destination || true Now this makes no sense outside of its very specific purpose. "Or true"? When would you use that? What it *actually* means is "attempt this copy, and if it fails, pretend it succeeded" (presumably because you have something aborting on error, like "set -e"). It's not boolean logic, and it's not really an alternate command for the failure case. The sh and bash system of logic works, there's no doubt about it. It takes "practicality beats purity" a very long way, and makes something where a fairly simple construct ("||" just means "if nonzero error code, execute this, otherwise don't") can do three or four barely-related jobs, which makes the language more compact. But none of this means that Python should, in any way, imitate it. ChrisA