Bool值的判断规则
来源:互联网 发布:ubuntu 压缩文件夹zip 编辑:程序博客网 时间:2024/06/05 19:32
The class bool is a subclass of the class int, and cannot be subclassed. bool类是int类的子类,但是不能被继承
既然bool是继承自int类型的所以很自然bool类型是支持数学运算的:
>>> True+True2>>> True*True1>>> False*True0
判定方法:
在python中and和or执行布尔逻辑运算,但返回的是实际值
and:如果为真,返回后值,如果为假,返回假值
or:如果为真,一个真,返回真值,两个真,返回前值,如果为假,返回后值
例:
>>> True or 7True>>> True and FalseFalse>>> False or 88>>> "happy" and "sad"'sad'>>> "happy" or "sad"'happy'>>> "" and "sad"''>>> "happy" and ""''
以下情况会被认为是False,其他的情况是True。
1. None
2. False
3. zero of any numeric type, for example, 0, 0L, 0.0, 0j.
4. any empty sequence, for example, ”, (), [].
5. any empty mapping, for example, {}.
6. instances of user-defined classes, if the class defines a nonzero() or len() method, when that method returns the integer zero or bool value False.
注意例子的变化:
>>> class newClass : pass>>> c=newClass()>>> bool(newClass)True>>> bool(c)True>>> class newClass2 : def __nonzero__(): return True>>> c2=newClass2()>>> bool(newClass2)True>>> bool(c2)Traceback (most recent call last): File "<pyshell#66>", line 1, in <module> bool(c2)TypeError: __nonzero__() takes no arguments (1 given)>>> class newClass6: def __nonzero__(self): return False>>> c6=newClass6()>>> bool(newClass6)True>>> bool(c6)False>>> class newClass7: def __nonzero__(self): return True>>> c7=newClass7()>>> bool(newClass7)True>>> bool(c7)True>>>>>> class newClass8: def __nonzero__(self): return 0>>> c8=newClass8()>>> bool(c8)False>>> class newClass9: def __nonzero__(self): return 7>>> c9=newClass9()>>> bool(c9)True>>>>>> class newClass10: def __len__(self): return 0>>> c10=newClass10()>>> bool(c10)False>>>如果既定义了__len__也定义了__nonzero__,会怎么样呢?>>> class newClass11: def __len__(self): return 0 def __nonzero__(self): return 7>>> c11=newClass11()>>> bool(c11)True>>> class newClass12: def __len__(self): return 7 def __nonzero__(self): return 0>>> c12=newClass12()>>> bool(c12)False>>>结果是以__nonzero__为主。
0 0
- Bool值的判断规则
- php判断bool值
- 简单bool函数式的判断
- PHP的bool值
- bool判断中的“!”陷阱
- bool值的字面值
- API 的 BOOL返回值
- bool 值的人性化输出
- Bool类型值的输出
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL 和 bool 的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL 与bool 的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL与bool 的区别
- jquery 获取url参数
- SlidingMenu创建侧滑栏的四种方式
- 浅谈开发中的MVVM模式及与MVP和MVC的区别
- asp.net webform 通过ajax取得json数据(一)
- 虚拟机加载类的过程
- Bool值的判断规则
- 结合Dialog的onClick()事件理解Android中的回调函数
- C#将图片字节流转为Base64直接放入html的img标签src属性中
- 微信返回上一页停留在上次点击的位置
- AngularJs 自定义ajax服务为请求添加加载动画
- 如何关闭Golang中的HTTP连接 How to Close Golang's HTTP connection
- 分布式存储系统GlusterFS初体验
- 分布式开源框架Hadoop介绍
- 偶然的一场电话面试感触