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

 

 

posted on 2016-01-05 11:02  蒋乐兴的技术随笔  阅读(992)  评论(0编辑  收藏  举报

导航