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


Groups > comp.lang.python > #97213 > unrolled thread

Re: Check if a given value is out of certain range

Started byRob Gaddi <rgaddi@technologyhighland.invalid>
First post2015-09-29 16:48 +0000
Last post2015-10-01 09:58 +0200
Articles 20 on this page of 52 — 22 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Check if a given value is out of certain range Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-09-29 16:48 +0000
    Re: Check if a given value is out of certain range Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-29 21:32 +0100
      Re: Check if a given value is out of certain range sohcahtoa82@gmail.com - 2015-09-30 11:31 -0700
        Re: Check if a given value is out of certain range Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-30 23:31 +0100
          Re: Check if a given value is out of certain range Steven D'Aprano <steve@pearwood.info> - 2015-10-02 04:18 +1000
            Re: Check if a given value is out of certain range John Gordon <gordon@panix.com> - 2015-10-01 18:20 +0000
              Re: Check if a given value is out of certain range Steven D'Aprano <steve@pearwood.info> - 2015-10-02 05:19 +1000
                Re: Check if a given value is out of certain range John Gordon <gordon@panix.com> - 2015-10-01 19:41 +0000
                  Re: Check if a given value is out of certain range Marko Rauhamaa <marko@pacujo.net> - 2015-10-01 22:59 +0300
                    Re: Check if a given value is out of certain range Emile van Sebille <emile@fenx.com> - 2015-10-01 13:15 -0700
                    Re: Check if a given value is out of certain range John Gordon <gordon@panix.com> - 2015-10-01 21:45 +0000
                      Re: Check if a given value is out of certain range Ian Kelly <ian.g.kelly@gmail.com> - 2015-10-01 16:03 -0600
                      Re: Check if a given value is out of certain range Chris Kaynor <ckaynor@zindagigames.com> - 2015-10-01 15:48 -0700
                      Re: Check if a given value is out of certain range Ian Kelly <ian.g.kelly@gmail.com> - 2015-10-01 16:01 -0600
                      Re: Check if a given value is out of certain range Steven D'Aprano <steve@pearwood.info> - 2015-10-03 20:29 +1000
                        Re: Check if a given value is out of certain range Bartc <bc@freeuk.com> - 2015-10-03 11:48 +0100
                        Re: Check if a given value is out of certain range Laura Creighton <lac@openend.se> - 2015-10-03 14:12 +0200
                          Re: Check if a given value is out of certain range Steven D'Aprano <steve@pearwood.info> - 2015-10-03 23:51 +1000
                            Re: Check if a given value is out of certain range wxjmfauth@gmail.com - 2015-10-03 07:19 -0700
                          Re: Check if a given value is out of certain range Steven D'Aprano <steve@pearwood.info> - 2015-10-05 00:47 +1100
                            Re: Check if a given value is out of certain range Rustom Mody <rustompmody@gmail.com> - 2015-10-04 19:28 -0700
                              Re: Check if a given value is out of certain range Rustom Mody <rustompmody@gmail.com> - 2015-10-04 19:32 -0700
                      Re: Check if a given value is out of certain range Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-10-03 11:49 -0400
            Re: Check if a given value is out of certain range Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-10-02 00:34 +0100
        Re: Check if a given value is out of certain range Michiel Overtoom <motoom@xs4all.nl> - 2015-10-05 10:18 +0200
          Re: Check if a given value is out of certain range Jussi Piitulainen <harvesting@makes.email.invalid> - 2015-10-05 13:08 +0300
            Re: Check if a given value is out of certain range Marko Rauhamaa <marko@pacujo.net> - 2015-10-05 14:42 +0300
              Re: Check if a given value is out of certain range Chris Angelico <rosuav@gmail.com> - 2015-10-06 00:10 +1100
                Re: Check if a given value is out of certain range Marko Rauhamaa <marko@pacujo.net> - 2015-10-05 16:25 +0300
        Re: Check if a given value is out of certain range Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-10-05 10:19 +0100
    Re: Check if a given value is out of certain range Random832 <random832@fastmail.com> - 2015-09-29 17:04 -0400
      Re: Check if a given value is out of certain range Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-09-30 07:57 +0200
    Re: Check if a given value is out of certain range Emile van Sebille <emile@fenx.com> - 2015-09-29 14:07 -0700
    Re: Check if a given value is out of certain range Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-29 15:07 -0600
      Re: Check if a given value is out of certain range Steven D'Aprano <steve@pearwood.info> - 2015-09-30 11:55 +1000
        Re: Check if a given value is out of certain range Random832 <random832@fastmail.com> - 2015-09-29 23:08 -0400
          Re: Check if a given value is out of certain range Steven D'Aprano <steve@pearwood.info> - 2015-09-30 13:14 +1000
            Re: Check if a given value is out of certain range Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-29 23:54 -0600
    Re: Check if a given value is out of certain range Tim Chase <python.list@tim.thechases.com> - 2015-09-29 18:44 -0500
      Re: Check if a given value is out of certain range alister <alister.nospam.ware@ntlworld.com> - 2015-09-30 09:41 +0000
        Re: Check if a given value is out of certain range John Gordon <gordon@panix.com> - 2015-09-30 15:42 +0000
        Re: Check if a given value is out of certain range Grant Edwards <invalid@invalid.invalid> - 2015-09-30 15:56 +0000
          Re: Check if a given value is out of certain range Marko Rauhamaa <marko@pacujo.net> - 2015-09-30 21:06 +0300
            Re: Check if a given value is out of certain range alister <alister.nospam.ware@ntlworld.com> - 2015-09-30 20:19 +0000
              Re: Check if a given value is out of certain range Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-30 14:46 -0600
                Re: Check if a given value is out of certain range alister <alister.nospam.ware@ntlworld.com> - 2015-10-01 08:33 +0000
                  Re: Check if a given value is out of certain range Chris Angelico <rosuav@gmail.com> - 2015-10-01 18:37 +1000
                    Re: Check if a given value is out of certain range alister <alister.nospam.ware@ntlworld.com> - 2015-10-01 08:48 +0000
                  Re: Check if a given value is out of certain range Ian Kelly <ian.g.kelly@gmail.com> - 2015-10-01 08:14 -0600
                Re: Check if a given value is out of certain range Steven D'Aprano <steve@pearwood.info> - 2015-10-02 04:59 +1000
              Re: Check if a given value is out of certain range Laura Creighton <lac@openend.se> - 2015-10-01 09:40 +0200
              Re: Check if a given value is out of certain range Laura Creighton <lac@openend.se> - 2015-10-01 09:58 +0200

Page 2 of 3 — ← Prev page 1 [2] 3  Next page →


#97395

FromRustom Mody <rustompmody@gmail.com>
Date2015-10-04 19:28 -0700
Message-ID<83b66c5c-25fe-48dd-b71c-5091803a1500@googlegroups.com>
In reply to#97392
On Sunday, October 4, 2015 at 7:18:11 PM UTC+5:30, Steven D'Aprano wrote:
> On Sat, 3 Oct 2015 10:12 pm, Laura Creighton wrote:
> 
> > Actually, the fact that adults have more difficulty processing
> > negations is one of the earliest things proven experimentally
> > in experimental psychology.
> 
> I don't think I've questioned that under some circumstances some negations
> can be hard to understand. I've certainly written my share of code
> involving negatives that I've had to refactor to understand. A typical
> example:
> 
> def function(arg, dontpreprocess=False):
>     """Perform function on arg. If dontpreprocess is not true, 
>     arg is preprocessed."""
>     if not dontpreprocess:
>         arg = preprocess(arg)
>     ...
> 
> And of course there are the legendary chains of negations:
> 
> don't not cancel the preprocessor suppressor
> 
> Does the preprocess run or not? :-)
> 
> But I don't think we can jump from a general observation about negations to
> the conclusion that a logical disjunction of two different comparisons is
> necessarily easier to understand than a negated chained comparison. Some
> negations are easy to understand:
> 
>     Don't touch that!
> 
> and some negations may technically be harder to understand, but in a
> practical sense the difference may be negligible:
> 
>     if x == 1: ...
> 
>     if x != 1: ...
> 
> 
> I refuse to believe that the second is *significantly* harder to reason
> about than the first.

[With hermeneutic/semantic hat firmly on]
one could make a case that
"!=" ≠ "not =" ≠ "≠"
[Back with python hat]
My preference:
not 1 <= x <= 10

[toc] | [prev] | [next] | [standalone]


#97396

FromRustom Mody <rustompmody@gmail.com>
Date2015-10-04 19:32 -0700
Message-ID<f06ae7c5-b573-44e3-9e62-9c31b504f97a@googlegroups.com>
In reply to#97395
On Monday, October 5, 2015 at 7:58:34 AM UTC+5:30, Rustom Mody wrote:
> On Sunday, October 4, 2015 at 7:18:11 PM UTC+5:30, Steven D'Aprano wrote:
> > and some negations may technically be harder to understand, but in a
> > practical sense the difference may be negligible:
> > 
> >     if x == 1: ...
> > 
> >     if x != 1: ...
> > 
> > 
> > I refuse to believe that the second is *significantly* harder to reason
> > about than the first.
> 
> [With hermeneutic/semantic hat firmly on]
> one could make a case that
> "!=" ≠ "not =" ≠ "≠"
> [Back with python hat]
> My preference:
> not 1 <= x <= 10

I take that back:
Unarys binding looser than binaries is "yuck" (in my book)
SO it would have to be
not (1 <= x <= 10)

[toc] | [prev] | [next] | [standalone]


#97381

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-10-03 11:49 -0400
Message-ID<mailman.367.1443887407.28679.python-list@python.org>
In reply to#97312
On Thu, 1 Oct 2015 21:45:46 +0000 (UTC), John Gordon <gordon@panix.com>
declaimed the following:

>In <87r3le1ht3.fsf@elektro.pacujo.net> Marko Rauhamaa <marko@pacujo.net> writes:
>

>> Wouldn't
>
>>    x < 0 or 10 < x
>
>> be even more visual?
>
>I don't know what you mean by "more visual".
>
>In my opinion, when comparing a variable to a constant, it's more natural
>to have the variable on the left and the constant on the right, so that's
>one strike against this code.
>
>Another strike is that the code isn't consistent with itself; it puts the
>variable on the left in the first comparison, then swaps to the right for
>the second comparison.

	To me, the above sample looks "right"... It is a test that "x is
outside" the range 0..10, so surrounding the range with "x" gives that
visual impression.

	While the "0 or 10" may be less pleasing than the "x is inside" a range
(0 < x < 10), if one did not have chained expressions the latter would
become "0 < x and x < 10".

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#97318

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-10-02 00:34 +0100
Message-ID<mailman.322.1443742526.28679.python-list@python.org>
In reply to#97296
On 01/10/2015 19:18, Steven D'Aprano wrote:
> On Thu, 1 Oct 2015 08:31 am, Mark Lawrence wrote:
>
>>> What is so "yuck" about that?  What would you do instead?  It seems like
>>> the best solution to me.  Easy to read, fast to execute.
>>>
>>
>> I have to parse those damn brackets and then figure out the inverted
>> logic. Give me x < 0 or x > 10 any day of the week.  When you're an old,
>> senile git like me, readability counts :-)
>
> With the greatest of respect Mark, I don't believe that for a second. Your
> sig line, which you have used without fail for more years than I can
> remember includes the phrase "ask not what our language can do for you". If
> you can understand that, I don't believe that you cannot figure out how to
> go from this:
>
> # x is within the range a to b
> a <= x < = b
>
> to this:
>
> # x is NOT within the range a to b
> not a <= x < = b
>
> You're certainly a senile old git if you think we're falling for that
> one :-)

Why do you think I never gamble at anything, it's a mug's game.

>
> P.S. in case you missed it, you don't actually need the params, since the
> precedence of not is lower than the other operators.

I confess that I did not bother to check.

>
> Did-I-include-sufficient-smileys-ly y'rs,
>

Not bad.  However after the big match coming up on Saturday evening UK 
time one of us will possibly be putting up ginormous quantities of 
smileys, matched by the ginormous quantity of grimaces from the other :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#97401

FromMichiel Overtoom <motoom@xs4all.nl>
Date2015-10-05 10:18 +0200
Message-ID<mailman.378.1444033178.28679.python-list@python.org>
In reply to#97251
Why not use a function?


    if outside(x, 0, 10):
        print("x has wrong value")
    else:
        print("x has good value")


where 'outside' is defined as:

    def outside(value, lowerbound, upperbound):
        return value < lowerbound or value > upperbound  

Greetings,

[toc] | [prev] | [next] | [standalone]


#97403

FromJussi Piitulainen <harvesting@makes.email.invalid>
Date2015-10-05 13:08 +0300
Message-ID<lf57fn1vd9a.fsf@ling.helsinki.fi>
In reply to#97401
Michiel Overtoom writes:

> Why not use a function?
>
>
>     if outside(x, 0, 10):
>         print("x has wrong value")
>     else:
>         print("x has good value")
>
>
> where 'outside' is defined as:
>
>     def outside(value, lowerbound, upperbound):
>         return value < lowerbound or value > upperbound  

Wouldn't that be more readable as:?

     def outside(value, lowerbound, upperbound):
         return not (lowerbound <= value <= upperbound)

Just wondering ...

[toc] | [prev] | [next] | [standalone]


#97407

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-10-05 14:42 +0300
Message-ID<87si5pv8we.fsf@elektro.pacujo.net>
In reply to#97403
Jussi Piitulainen <harvesting@makes.email.invalid>:

> Michiel Overtoom writes:
>
>> Why not use a function?
>>
>>
>>     if outside(x, 0, 10):
>>         print("x has wrong value")
>>     else:
>>         print("x has good value")
>>
>>
>> where 'outside' is defined as:
>>
>>     def outside(value, lowerbound, upperbound):
>>         return value < lowerbound or value > upperbound  
>
> Wouldn't that be more readable as:?
>
>      def outside(value, lowerbound, upperbound):
>          return not (lowerbound <= value <= upperbound)

Why not use a fricking class framework:

    class Endpoint:
        def __init__(self, value):
            self.value = value

    class IncludedEndpoint(Endpoint):
        def above(self, x):
            return x > self.value

        def below(self, x):
            return x < self.value

    class ExcludedEndpoint(Endpoint):
        def above(self, x):
            return x >= self.value

        def below(self, x):
            return x <= self.value

    class Range:
        def __init__(self, lower_bound, upper_bound):
            self.lower_bound = lower_bound
            self.upper_bound = upper_bound

        def outside(self, x):
            return self.lower_bound.below(x) or self.upper_bound.above(x)

        def inside(self, x):
            return not self.outside(x)

Then, we could simply have:

    if Range(IncludedEndpoint(0), IncludedEndpoint(10)).outside(x):
        print("x has wrong value")
    else:
        print("x has good value")

Just for the sake of readability, I mean...


Marko

[toc] | [prev] | [next] | [standalone]


#97408

FromChris Angelico <rosuav@gmail.com>
Date2015-10-06 00:10 +1100
Message-ID<mailman.387.1444050603.28679.python-list@python.org>
In reply to#97407
On Mon, Oct 5, 2015 at 10:42 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Why not use a fricking class framework:
>
> Then, we could simply have:
>
>     if Range(IncludedEndpoint(0), IncludedEndpoint(10)).outside(x):
>         print("x has wrong value")
>     else:
>         print("x has good value")
>
> Just for the sake of readability, I mean...

Hold on a moment! You can't seriously be contemplating instantiating
those classes directly, can you?!? You need to go through the
ThreadedRangeFactory, passing it a couple of EndpointFactory
subclasses to suggest which kinds of endpoints to employ, and finally
use operator overloading to wait for the factory thread to finish its
work. *THEN* you can use this Range object.

ChrisA
has just been reading http://thedailywtf.com/articles/eins-zwei-zuffa

[toc] | [prev] | [next] | [standalone]


#97409

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-10-05 16:25 +0300
Message-ID<87oagdv44w.fsf@elektro.pacujo.net>
In reply to#97408
Chris Angelico <rosuav@gmail.com>:

> On Mon, Oct 5, 2015 at 10:42 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
>> Why not use a fricking class framework:
>>
>> Then, we could simply have:
>>
>>     if Range(IncludedEndpoint(0), IncludedEndpoint(10)).outside(x):
>>         print("x has wrong value")
>>     else:
>>         print("x has good value")
>>
>> Just for the sake of readability, I mean...
>
> Hold on a moment! You can't seriously be contemplating instantiating
> those classes directly, can you?!? You need to go through the
> ThreadedRangeFactory, passing it a couple of EndpointFactory
> subclasses to suggest which kinds of endpoints to employ, and finally
> use operator overloading to wait for the factory thread to finish its
> work. *THEN* you can use this Range object.
>
> ChrisA
> has just been reading http://thedailywtf.com/articles/eins-zwei-zuffa

Pointing out flaws in my code is totally unprofessional.


Marko

[toc] | [prev] | [next] | [standalone]


#97402

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-10-05 10:19 +0100
Message-ID<mailman.381.1444036829.28679.python-list@python.org>
In reply to#97251
On 05/10/2015 09:18, Michiel Overtoom wrote:
>
> Why not use a function?
>
>
>      if outside(x, 0, 10):
>          print("x has wrong value")
>      else:
>          print("x has good value")
>
>
> where 'outside' is defined as:
>
>      def outside(value, lowerbound, upperbound):
>          return value < lowerbound or value > upperbound
>
> Greetings,
>

Please wash your mouth out with soap, burn precious clock cycles just to 
make code readable, never I say :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#97217

FromRandom832 <random832@fastmail.com>
Date2015-09-29 17:04 -0400
Message-ID<mailman.247.1443560681.28679.python-list@python.org>
In reply to#97213
On Tue, Sep 29, 2015, at 16:32, Mark Lawrence wrote:
> On 29/09/2015 17:48, Rob Gaddi wrote:
> > On Tue, 29 Sep 2015 10:16:04 +0530, Laxmikant Chitare wrote:
> >
> >> Hi,
> >>
> >> I know there is an elegant way to check if a given value is within
> >> certain range.
> >> Example - To check if x is between zero and ten, I can do 0 < x 10.
> >>
> >> Is there any similar elegant way to check if a value is out of certain
> >> range?
> >> Example - To check if x is either less than zero or greater than ten?
> >> Right now I am using x < 0 or x > 10.
> >>
> >> Regards,
> >> Laxmikant
> >
> > not (0 <= x <= 10)
> >
> 
> Yuck.

How about x not in range(11)?

[toc] | [prev] | [next] | [standalone]


#97235

FromLuca Menegotto <otlucaDELETE@DELETEyahoo.it>
Date2015-09-30 07:57 +0200
Message-ID<muftjv$9cr$1@speranza.aioe.org>
In reply to#97217
Il 29/09/2015 23:04, Random832 ha scritto:

> How about x not in range(11)?
>

Remember: simpler is better.

-- 
Ciao!
Luca

[toc] | [prev] | [next] | [standalone]


#97218

FromEmile van Sebille <emile@fenx.com>
Date2015-09-29 14:07 -0700
Message-ID<mailman.248.1443560864.28679.python-list@python.org>
In reply to#97213
On 9/29/2015 2:04 PM, Random832 wrote:
> On Tue, Sep 29, 2015, at 16:32, Mark Lawrence wrote:

>>>
>>> not (0 <= x <= 10)
>>>
>>
>> Yuck.
>
> How about x not in range(11)?


x = 5.5

Emile


[toc] | [prev] | [next] | [standalone]


#97219

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-09-29 15:07 -0600
Message-ID<mailman.249.1443560905.28679.python-list@python.org>
In reply to#97213
On Tue, Sep 29, 2015 at 3:04 PM, Random832 <random832@fastmail.com> wrote:
> How about x not in range(11)?

That's fine as long as x is known to only take integral values.

[toc] | [prev] | [next] | [standalone]


#97222

FromSteven D'Aprano <steve@pearwood.info>
Date2015-09-30 11:55 +1000
Message-ID<560b412e$0$1616$c3e8da3$5496439d@news.astraweb.com>
In reply to#97219
On Wed, 30 Sep 2015 07:07 am, Ian Kelly wrote:

> On Tue, Sep 29, 2015 at 3:04 PM, Random832 <random832@fastmail.com> wrote:
>> How about x not in range(11)?
> 
> That's fine as long as x is known to only take integral values.

It's not fine. In Python 2, it's painfully slow and inefficient, both
memory-wise and algorithmically:

-1 in range(100000000)  # test 0 <= -1 <= 100000000

This first creates a list of 100000000 integers, then compares each and
every one of them against -1 before returning False.

Using xrange instead at least avoids building the list first, but it still
compares -1 against each value.

Testing a numeric value within a certain range of values should be constant
time and constant memory. It should be *fast*. Using range in Python 2 is
none of those things.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#97228

FromRandom832 <random832@fastmail.com>
Date2015-09-29 23:08 -0400
Message-ID<mailman.256.1443582541.28679.python-list@python.org>
In reply to#97222
Steven D'Aprano <steve@pearwood.info> writes:
> It's not fine. In Python 2,
>...
> Testing a numeric value within a certain range of values should be constant
> time and constant memory. It should be *fast*. Using range in Python 2 is
> none of those things.

I wasn't aware we were discussing Python 2.

[toc] | [prev] | [next] | [standalone]


#97230

FromSteven D'Aprano <steve@pearwood.info>
Date2015-09-30 13:14 +1000
Message-ID<560b53a1$0$1585$c3e8da3$5496439d@news.astraweb.com>
In reply to#97228
On Wed, 30 Sep 2015 01:08 pm, Random832 wrote:

> Steven D'Aprano <steve@pearwood.info> writes:
>> It's not fine. In Python 2,
>>...
>> Testing a numeric value within a certain range of values should be
>> constant time and constant memory. It should be *fast*. Using range in
>> Python 2 is none of those things.
> 
> I wasn't aware we were discussing Python 2.

Was there something in the OP's question that suggested to you that we were
only discussing Python 3?

Python 2.7 is still the main version used by most people.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#97234

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-09-29 23:54 -0600
Message-ID<mailman.260.1443592484.28679.python-list@python.org>
In reply to#97230
On Tue, Sep 29, 2015 at 9:14 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Wed, 30 Sep 2015 01:08 pm, Random832 wrote:
>
>> Steven D'Aprano <steve@pearwood.info> writes:
>>> It's not fine. In Python 2,
>>>...
>>> Testing a numeric value within a certain range of values should be
>>> constant time and constant memory. It should be *fast*. Using range in
>>> Python 2 is none of those things.
>>
>> I wasn't aware we were discussing Python 2.
>
> Was there something in the OP's question that suggested to you that we were
> only discussing Python 3?
>
> Python 2.7 is still the main version used by most people.

As far as I'm concerned, Python is Python 3. I'm aware that the check
is inefficient in Python 2, but I tire of constantly pointing out
Python 2 as the exception to everything.

[toc] | [prev] | [next] | [standalone]


#97224

FromTim Chase <python.list@tim.thechases.com>
Date2015-09-29 18:44 -0500
Message-ID<mailman.252.1443578914.28679.python-list@python.org>
In reply to#97213
On 2015-09-29 21:32, Mark Lawrence wrote:
> On 29/09/2015 17:48, Rob Gaddi wrote:
> >> Is there any similar elegant way to check if a value is out of
> >> certain range?
> >> Example - To check if x is either less than zero or greater than
> >> ten? Right now I am using x < 0 or x > 10.
> >
> > not (0 <= x <= 10)
> 
> Yuck.

Not sure there's much "yuck" to be had there.  It's succinct, easy to
read, and correct.  The only improvement might be if you have things
to do in both cases, in which case remove the "not" and set the
clauses accordingly:

  if 0 <= x <= 10:
    success_within_range(x)
  else:
    fail_out_of_bounds(x)

-tkc


[toc] | [prev] | [next] | [standalone]


#97240

Fromalister <alister.nospam.ware@ntlworld.com>
Date2015-09-30 09:41 +0000
Message-ID<mugaov$79k$1@speranza.aioe.org>
In reply to#97224
On Tue, 29 Sep 2015 18:44:33 -0500, Tim Chase wrote:

> On 2015-09-29 21:32, Mark Lawrence wrote:
>> On 29/09/2015 17:48, Rob Gaddi wrote:
>> >> Is there any similar elegant way to check if a value is out of
>> >> certain range?
>> >> Example - To check if x is either less than zero or greater than
>> >> ten? Right now I am using x < 0 or x > 10.
>> >
>> > not (0 <= x <= 10)
>> 
>> Yuck.
> 
> Not sure there's much "yuck" to be had there.  It's succinct, easy to
> read, and correct.  The only improvement might be if you have things to
> do in both cases, in which case remove the "not" and set the clauses
> accordingly:
> 
>   if 0 <= x <= 10:
>     success_within_range(x)
>   else:
>     fail_out_of_bounds(x)
> 
> -tkc

I would stick with the OP's current solution

Readability Counts!



-- 
BUFFERS=20 FILES=15 2nd down, 4th quarter, 5 yards to go!

[toc] | [prev] | [next] | [standalone]


Page 2 of 3 — ← Prev page 1 [2] 3  Next page →

Back to top | Article view | comp.lang.python


csiph-web