Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3a.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:not': 0.03; 'operator': 0.03; 'odd': 0.07; 'subject:two': 0.07; 'variables': 0.07; 'subject:None': 0.09; 'subtle': 0.09; 'way:': 0.09; 'cc:addr :python-list': 0.11; 'bug': 0.12; 'assume': 0.14; 'wrote': 0.14; '"is': 0.16; 'background,': 0.16; 'chained': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mean,': 0.16; 'sense:': 0.16; 'two,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; 'written': 0.21; 'seems': 0.21; '(the': 0.22; 'cc:addr:python.org': 0.22; 'mathematical': 0.24; 'cc:2**0': 0.24; 'order.': 0.26; 'this:': 0.26; 'second': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'points': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'agreed.': 0.31; 'are.': 0.31; 'constant': 0.31; "d'aprano": 0.31; 'operators': 0.31; 'steven': 0.31; 'way?': 0.31; 'quite': 0.32; 'open': 0.33; 'checking': 0.33; 'sense': 0.34; 'maybe': 0.34; 'problem': 0.35; "can't": 0.35; 'good.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'i.e.': 0.36; 'useful': 0.36; 'two': 0.37; 'clear': 0.37; 'checks': 0.38; 'e.g.': 0.38; 'fact': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'skip:u 10': 0.60; 'strictly': 0.61; "you're": 0.61; 'first': 0.61; 'high': 0.63; 'more': 0.64; 'different': 0.65; '30,': 0.65; 'within': 0.65; 'between': 0.67; 'mar': 0.68; 'line,': 0.68; 'saw': 0.77; 'low': 0.83; 'bounded': 0.84; "it'd": 0.84; 'maths': 0.84; '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; bh=qJPeGrMxtfv336JuY3rKLQxEfsmtTZ1AZHByKUVb69s=; b=hMNE6itipBrREkqItRmlXU0UbtD7KZ2gDHQMNF2V8PWQPyP/0iRmT5sd+usLoF58OP Q2K3pJPwAcIjCbVYRLaor6s85MzTey8ZqWXsTBYGlQ8uzubKAytwO05mwn6KAyCEdt+u VzJME8umPVrxQNC09Zi8sJywE5VD/v+T8TDBPsTOp1ZxBKH0/otCzuCoN2C2cpltHCwB nK8mn5+To06audcpwcJcc6pkKK6nudDdW1QnSkw/Ng34jDMmJ36+wV7aKfFQIXkrc6cZ gGJE2zdhSXcaOb9V9M2zfE3cofYvhE7zfvqME0AJ/9bJTqEI/OP2hqVnp530jEdk7o98 D/kA== MIME-Version: 1.0 X-Received: by 10.66.150.69 with SMTP id ug5mr17815877pab.55.1396160254569; Sat, 29 Mar 2014 23:17:34 -0700 (PDT) In-Reply-To: <5337b182$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <0245aca0-c6b7-493a-aa52-2c3ef6462dbd@googlegroups.com> <5337195f$0$29994$c3e8da3$5496439d@news.astraweb.com> <53377b9e$0$29994$c3e8da3$5496439d@news.astraweb.com> <5337b182$0$29994$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 30 Mar 2014 17:17:34 +1100 Subject: Re: checking if two things do not equal None From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396160262 news.xs4all.nl 2902 [2001:888:2000:d::a6]:55719 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69373 On Sun, Mar 30, 2014 at 4:54 PM, Steven D'Aprano wrote: > On Sun, 30 Mar 2014 13:15:18 +1100, Chris Angelico wrote: > >> Chained comparisons where you're checking a single variable against two >> constants make perfect sense: >> >> 2 < x < 5 >> >> Chained comparisons where you check a single constant against two >> variables don't, so much: >> >> x < 2 < y >> >> What exactly does that mean, and why is it written that way? > > It checks that 2 is strictly bounded between x on the left and y on the > right, i.e. that 2 is inside the open interval x...y. I don't know why > you think that's unclear. But then I do have a maths background and I'm > used to chaining comparisons. > > Write it like this: > > low = x > high = y > a = 2 > > low < a < high > > Does that make more sense? Well-chosen names are good. The fact that a is > a constant rather than a variable is no big deal: > > low < 2 < high The problem isn't that I can't see what the comparisons are. It makes very good sense to bound a variable within constants; but you already know exactly where 2 is on the number line, so asking "Is 2 between these two variables" seems a bit odd. Maybe it's less so with the strong mathematical background, but it seems odd to me. >> It'd be more useful but less clear if one of the conditions points the >> other way: >> >> x < 2 > y >> >> which checks that they're both less than two, > > which is quite different from what you wrote the first time. > > >> but IMO in a less-than-clear way. > > That's an understatement. If I saw code chaining comparisons in that > fashion, I would assume the second operator > was a typo. > > Chaining less-than and greater than operators should, for clarity, always > be written in a single order. E.g. a <= b < c < d, not a <= b < d > c. > > (The second contains a subtle bug too.) Agreed. ChrisA