开发者

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 ph开发者_运维问答p 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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜