Skip to content Skip to sidebar Skip to footer

Python - Any Way Of Checking If Number 0 Or False

If I were to set a variable as False, it is read as being equal to zero. Is there any way I can carry out a check to see if the variable if actually False or if it's the number 0.

Solution 1:

You can test if the object is the False singleton value, by testing for identity:

if Spam isFalse:

You should really refactor your code to not have to rely on the type here; None is a better false-y sentinel value to use, yes.

In general, use None as a sentinel meaning 'no value set', or if you should support None as well, use a unique object as a sentinel:

_sentinel = object()

# ...if option is _sentinel:
    # no value set

Post a Comment for "Python - Any Way Of Checking If Number 0 Or False"