Python内置函数学习(6)-bool(x)

来源:互联网 发布:mac程序删除 编辑:程序博客网 时间:2024/05/17 18:12

英文解释:
Return a Boolean value, i.e. one of True or False. x is converted using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is a subclass of int. Class bool cannot be subclassed further. Its only instances are False and True.
New in version 2.2.1.
Changed in version 2.3: If no argument is given, this function returns False.
中文翻译:
返回布尔值,可以为True或者False,利用标准真值检测过程将x转换为布尔值,如果x是false或者省略,则返回False,否则返回True。bool也是一个类,它是整形int的子类,bool类不能再派生子类了,它的实例是能是False和True

常用的返回空的情况有:x为None,False,0,空字典,空列表,空元素,空字符串

>>> bool()False>>> bool([])False>>> bool(())False>>> bool({})False>>> bool(0)False>>> bool(None)False>>> bool(False)False>>> bool('')False

注意字符串为一个空格字符串时,bool()值为True,x为非零数字时返回值也为True

>>> bool(0.1)True>>> bool(-.01)True>>> bool(123)True>>> bool(' ')True

注意,如果一个对象有多个元素,每个元素的bool值为False,但是这个对象的bool返回值却为True

>>> a=[0,None,False]>>> bool(a)True>>> b=(0,'',[])>>> bool(b)True>>> c=[[],(),{}]>>> bool(c)True>>> 
0 0