真值测试
True==0 # False
True==1 # True
True==2 # False
False==0 # True
False==1 # False
False==2 # False
python中的任何对象都有真值,以下对象的真值为false:
- None
- False
- zero of any numeric type,for example 0,0.0,0j.
- any empty sequence,for example,'',(),[].
- any empty mapping,for example,{}
- instances of user-defined classes, if the class defines a __bool__() or __len__() method, when that method returns the integer zero or bool value False.
其它所有对象的真值都被视为 true 。一些返回布尔值的操作和内置函数,总是返回0或者False来表示真值为false,返回1或者True来表示真值为true. (例外: 布尔操作 or 和 and 总是返回其操作数)