1 > 0 == True -> False

Chris Angelico rosuav at gmail.com
Thu Jan 30 08:55:44 EST 2014


On Fri, Jan 31, 2014 at 12:40 AM, Thibault Langlois
<thibault.langlois at gmail.com> wrote:
> The recommendations to student are 1) do not assume True == 1 and do not use operator chaining.

Not "do not use", but "do not misuse". Python's operator chaining is
awesome for bounds checking:

if 3 < x < 20:
  print("x is between 3 and 20, exclusive")

You can even, since it short-circuits, do crazy stuff like:

x = random.randrange(30)
if int(input("Min: ")) < x < int(input("Max: ")):
    print("It's within those bounds.")

It won't ask for a maximum if it's already failed to be within the
minimum. (Okay, don't do this one. Not good code. Heh.)

But don't use operator chaining when you don't mean it to behave that
way. That's all!

ChrisA



More information about the Python-list mailing list