Bob
Bob

Reputation: 41

Checking for value within same type in python

a = 0
if a == False:
   print a

in php I can say:

$a = 0;
if $a === false {
    echo $a;
}

The triple === in php check for the value within the same type, thus making the integer 0 not be read as a boolean value False How can I do this in python? I would like to differentiate between 0 the integer and False the boolean value in a simple if statement.

Upvotes: 4

Views: 259

Answers (2)

Brian McKenna
Brian McKenna

Reputation: 46228

You should use the is keyword in that case. It's the identity operator, the same as === in PHP.

>>> a = 0
>>> if a is False:
...     print a
...
>>> 

Upvotes: 5

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

type() will give you the type of an object. But if you're worried about distinguishing between 0 and False then perhaps you should use None instead.

Upvotes: 0

Related Questions