None是Python中的一个关键字,None本身也是个一个数据类型,而这个数据类型就是None,它可0.空字符串以及false均不一样,这些都只是对象,而None也是一个类. 给个bool测试: val = None if val: print "None is true" else: print "None is not true" #输出None is not true
在Python中,None.空列表[].空字典{}.空元组().0等一系列代表空和无的对象会被转换成False.除此之外的其它对象都会被转化成True. 1.0等于False,这点要注意. 2.空的list判断: if 0 == len(list) 或者 if list #空的list相当于False 而if len(list)则表示list不为空时,因为len(list)在等于0的时候为False,其他情况下为True. 3.判断是否为None: if X is None if not X
这个其实在Python文档当中有写了,为了准确起见,我们先引用Python文档当中的原文: In the context of Boolean operations, and also when expressions are used bycontrol flow statements, the following values are interpreted as false:False, None, numeric zero of all types, and empty strings
1.像(),[],{}这三个是可以通过bool(()),bool([]),bool({})转化为bool值的:且它们转化后的结果为False.但是这三个值它本身并不等于False.切记不可以与False 直接进行比较. #!/usr/bin/python #!coding:utf-8 import sys if __name__ =="__main__": falseList=[0,False,'',(),[],{}] print("the list is :[[0,Fals