Check if a given value is out of certain range

John Gordon gordon at panix.com
Thu Oct 1 17:45:46 EDT 2015


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

> > I wasn't commenting directly to the "ask not..." quote; I was
> > referring upthread to the choice between
> >
> >     not 0 <= x <= 10
> >
> > and
> >
> >     x < 0 or x > 10
> >
> > Both are of course understandable, but in my opinion, the latter one
> > takes slightly less effort to grok.

> 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.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list