python bool值要注意的一些地方

时间:2023-03-09 15:48:48
python bool值要注意的一些地方

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,False,'',(),[],{}]") print('--------------------------------------------section 001')
for ix in range(len(falseList)):
print('the id of item {0} the id when it convert to bool {1}'.format(id(falseList[ix]),id(bool(falseList[ix]))))
print('--------------------------------------------section 002')
print("0==False ? {0}".format(0==False))
print("False==False ? {0}".format(0==False))
print('()==False ? {0}'.format(() == False))
print('[]==Flase ? {0}'.format([]==False))
print('{{}}==Flase ? {0}'.format({}==False))

python bool值要注意的一些地方