开发者

Checking for value within same type in python

开发者 https://www.devze.com 2022-12-17 11:30 出处:网络
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
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

精彩评论

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

关注公众号