Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69373

Re: checking if two things do not equal None

References (2 earlier) <lh7cb4$ntu$2@news.albasani.net> <roy-B5E8CC.17072029032014@news.panix.com> <53377b9e$0$29994$c3e8da3$5496439d@news.astraweb.com> <mailman.8709.1396145720.18130.python-list@python.org> <5337b182$0$29994$c3e8da3$5496439d@news.astraweb.com>
Date 2014-03-30 17:17 +1100
Subject Re: checking if two things do not equal None
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.8712.1396160262.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Mar 30, 2014 at 4:54 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

checking if two things do not equal None contact.trigon@gmail.com - 2014-03-29 11:56 -0700
  Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-29 19:05 +0000
    Re: checking if two things do not equal None contact.trigon@gmail.com - 2014-03-29 12:23 -0700
    Re: checking if two things do not equal None Lele Gaifax <lele@metapensiero.it> - 2014-03-29 20:24 +0100
    Re: checking if two things do not equal None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-29 22:01 +0100
      Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 17:07 -0400
        Re: checking if two things do not equal None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-29 22:55 +0100
          Re: checking if two things do not equal None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-29 23:02 +0100
        Re: checking if two things do not equal None Dave Angel <davea@davea.name> - 2014-03-29 18:01 -0400
          Re: checking if two things do not equal None contact.trigon@gmail.com - 2014-03-29 16:20 -0700
        Re: checking if two things do not equal None Tim Chase <python.list@tim.thechases.com> - 2014-03-29 17:36 -0500
          Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-30 02:24 +0000
            Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 22:43 -0400
              Re: checking if two things do not equal None Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-03-29 22:04 -0500
        Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 18:41 -0400
        Re: checking if two things do not equal None Tim Chase <tim@thechases.com> - 2014-03-29 17:46 -0500
          Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 18:51 -0400
        Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 10:17 +1100
        Re: checking if two things do not equal None Tim Chase <python.list@tim.thechases.com> - 2014-03-29 20:19 -0500
        Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 12:37 +1100
        Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-30 02:04 +0000
          Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 13:15 +1100
            Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 22:39 -0400
              Re: checking if two things do not equal None Rustom Mody <rustompmody@gmail.com> - 2014-03-29 19:54 -0700
                Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-30 06:08 +0000
                Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-30 08:21 -0400
                Re: checking if two things do not equal None MRAB <python@mrabarnett.plus.com> - 2014-03-30 14:58 +0100
                Re: checking if two things do not equal None Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-31 11:48 +1300
              Re: checking if two things do not equal None Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-30 19:41 +1300
                Re: checking if two things do not equal None Marko Rauhamaa <marko@pacujo.net> - 2014-03-30 10:37 +0300
            Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-30 05:54 +0000
              Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 17:17 +1100
              Re: checking if two things do not equal None Ben Finney <ben+python@benfinney.id.au> - 2014-03-30 17:52 +1100
              Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 18:36 +1100
      Re: checking if two things do not equal None Ethan Furman <ethan@stoneleaf.us> - 2014-03-29 16:20 -0700
  Re: checking if two things do not equal None Terry Reedy <tjreedy@udel.edu> - 2014-03-29 19:02 -0400
  Re: checking if two things do not equal None Jeremy Sanders <jeremy@jeremysanders.net> - 2014-03-31 09:56 +0200
    Re: checking if two things do not equal None Abe <contact.trigon@gmail.com> - 2014-03-31 10:28 -0700
      Re: checking if two things do not equal None Moritz Emanuel Beber <moritz.beber@gmail.com> - 2014-03-31 21:22 +0200

csiph-web