tags:

views:

44

answers:

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

A: 

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.

Ignacio Vazquez-Abrams
+5  A: 

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