• Python bool 详解 (扒源码)


    目录

    一、布尔类型描述

    二、布尔运算

    三、比较运算

    四、总结


    一、布尔类型描述

            布尔类型是计算机中最基本的类型,它是计算机二进制世界的体现,一切都是 01 。Python中的布尔类型只有两种值: TrueFalse

    (注意:首字母都是大写,与C++、JavaScript中的小写有所不同)

            布尔类型回答的是 是非 问题,那么什么情况下是 True ,什么情况下是 False 呢? Python里面实现了一个 类型对象 叫做 bool ,bool是一个 int 的子类,内置的 TrueFalse 就是bool仅有的两个实例对象。

            python 中布尔值使用常量 True False来表示;注意 T F 大小写

            boolint 的子类(继承 int ),故 True == 1 False == 0 是会返回 Ture

            bool 类型只有两种状态真或假

    使用bool我们就可以对对象进行布尔真假判断:

    为假的情况有:

    1. print(bool(None))
    2. print(bool(0))
    3. print(bool(0.0))
    4. print(bool(0j)) # 虚数
    5. print(bool(Decimal(0))) # 0 from decimal import Decimal
    6. print(bool(Fraction(0, 1))) # 0 from fractions import Fraction
    7. print(bool('')) # 空字符串
    8. print(bool({})) # 空字典
    9. print(bool(set())) # 空集合
    10. print(bool(())) # 空元组
    11. print(bool([])) # 空列表

     

    二、布尔运算

    优先级:not > and > or

    运算表达式结果说明
    或运算x or y如果x为False则取决于y;如果x为True则不考虑y(1)
    与运算x and y如果x为False则不考虑y;如果x为True则取决于y(2)
    非运算not x如果x为False则为True,否则为False(2)

    说明:

    (1) or 是一种“短路运算符”,只有当第一个为False时才去验证第二个。即:两个变量只要有一个为True则为True。

    (2) and 也是种“短路运算符”,只有当第一个为True时才去验证第二个。即:两个变量都为True时结果才为True。

    (3) not 的优先级比非布尔运算符底,所以 not a == b 解释为 not (a == b) ,并且 a == not b 是语法错误。

    1. print(1 > 2 or 2 > 1)
    2. print(1 < 2 and 1 < 3)
    3. print(not 1 == 2)

    三、比较运算

            前面提到,布尔值反应的是“是非”,有比较才有是非。Python中有8中比较运算。它们有相同的优先级,比布尔运算的优先级高。比较运算符可以任意的连写,比如: x < y <=z 相当于 x < y and y <= z

    运算含义
    <小于
    <=小于等于
    >大于
    >=大于等于
    ==等于
    !=不等于
    is是对象
    is not不是对象

    四、总结

    布尔类型(True, False)表示“是非”,是比较运算的结果,是条件判断的结果,从而决定程序的流程和分支走向。

    默认情况下,所有类型都可以转化为布尔类型

     

    1. from decimal import Decimal
    2. from fractions import Fraction
    3. print(bool(None))
    4. print(bool(0))
    5. print(bool(0.0))
    6. print(bool(0j)) # 虚数
    7. print(bool(Decimal(0))) # 0 from decimal import Decimal
    8. print(bool(Fraction(0, 1))) # 0 from fractions import Fraction
    9. print(bool('')) # 空字符串
    10. print(bool({})) # 空字典
    11. print(bool(set())) # 空集合
    12. print(bool(())) # 空元组
    13. print(bool([])) # 空列表
    14. print(1 > 2 or 2 > 1)
    15. print(1 < 2 and 1 < 3)
    16. print(not 1 == 2)

     五、源码

    bool(x) -> bool
    
    Returns True when the argument x is true, False otherwise.
    The builtins True and False are the only two instances of the class bool.
    The class bool is a subclass of the class int, and cannot be subclassed.
    1. class bool(int):
    2. """
    3. bool(x) -> bool
    4. Returns True when the argument x is true, False otherwise.
    5. The builtins True and False are the only two instances of the class bool.
    6. The class bool is a subclass of the class int, and cannot be subclassed.
    7. """
    8. def __and__(self, *args, **kwargs): # real signature unknown
    9. """ Return self&value. """
    10. pass
    11. def __init__(self, x): # real signature unknown; restored from __doc__
    12. pass
    13. @staticmethod # known case of __new__
    14. def __new__(*args, **kwargs): # real signature unknown
    15. """ Create and return a new object. See help(type) for accurate signature. """
    16. pass
    17. def __or__(self, *args, **kwargs): # real signature unknown
    18. """ Return self|value. """
    19. pass
    20. def __rand__(self, *args, **kwargs): # real signature unknown
    21. """ Return value&self. """
    22. pass
    23. def __repr__(self, *args, **kwargs): # real signature unknown
    24. """ Return repr(self). """
    25. pass
    26. def __ror__(self, *args, **kwargs): # real signature unknown
    27. """ Return value|self. """
    28. pass
    29. def __rxor__(self, *args, **kwargs): # real signature unknown
    30. """ Return value^self. """
    31. pass
    32. def __xor__(self, *args, **kwargs): # real signature unknown
    33. """ Return self^value. """
    34. pass

  • 相关阅读:
    vue 监听dom元素尺寸大小改变
    C语言的标准函数库:断言库assert.h
    微信怎么发状态?简单教程,一学就会!
    MoneyPrinterPlus:AI自动短视频生成工具-腾讯云配置详解
    SMB over QUIC帮助实现文件服务器在公网安全共享
    寻找两个正序数组的中位数
    QGIS编译(跨平台编译)之五十五:Qt Creator环境下libdxfrw库的pro文件
    css PC端弹窗时禁止底部页面滚动
    【刷题】怎么样才能正确的迎接面试?
    R语言使用plot函数可视化数据散点图,使用cex.main参数自定义设置主标题的字体大小
  • 原文地址:https://blog.csdn.net/c_lanxiaofang/article/details/126445601