Solved

if x == True vs if x

NoahLoopsOP

My code has if is_ready == True:. A reviewer told me to write if is_ready:. Why?

7

Comments 5

Join the discussion

Already have an account? Log in

  1. HenryWrites Solutionreplies: 2

    if is_ready: already checks whether the value is truthy, so == True is redundant. It's also stricter in a way you might not want: 1 == True is True, but [1] == True is False even though [1] is truthy.

    PEP 8 explicitly recommends if is_ready: for booleans.

    7
      1. LucasVD

        And not just if not x: when 0 or "" are valid values! That one bit me with a sensor reading of 0.

        5
      1. Mateusz Space Python team

        Good point — the "Missing reading" challenge is built around exactly that 0 vs None trap.

        4