设为首页 加入收藏

TOP

深入理解 Python and 逻辑运算符(踩坑)(一)
2023-09-09 10:25:28 】 浏览:116
Tags:Python and 踩坑

1. 引子

def enabled() -> bool:
    a = ["a,"b"]
	b = True
    c = False
    return (b and c) or (b and a)

以上代码返回什么?

实际生产项目踩到的坑,也怪自己没理解到未,才疏学浅!!!

想当然的以为 python 自己会做真值判断了。其实真值判断是在 if 条件语句时会生效,但在普通的 and 的运算符下有另外一个规则。

2. python bool 类型简述

“The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings ‘False’ or ‘True’ are returned, respectively.”

“布尔类型是整数类型的一个子类型,在几乎所有的上下文环境中布尔值的行为类似于值0和1,例外的是当转换为字符串时,会分别将字符串”False“或”True“返回。“

就python而言,True,1和1.0都表示相同的字典键

>>> True == 1 == 1.0
True
>>> (hash(True), hash(1), hash(1.0))
(1, 1, 1)

>>> issubclass(bool, int)
True
>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
>>> int(True)
1
>>> int(False)
0

>>> help(bool)

Help on class bool in module builtins:

class bool(int)
    |  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.

3. bool类型之间的 and 运算

and 运算符有两个操作数,可以是 bool,object,或一个组合

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False

以上示例表明,仅当表达式中的两个操作数都为 true 时,和表达式才返回 True。由于 and 运算符需要两个操作数来构建表达式,因此它是一个二元运算符。

当两边操作书都为 bool 类型时,二元操作运算结果如下。

operand1 operand2 operand1 and operand2
True True True
True False False
False False False
False True False

当然,以上的结果也适用于两边操作数一个 bool 表达式的情况。

expression1 and expression2

>>> 5>3 and 5==3+2
True

4. 不同类型之间的 and 运算

可以使用 and 运算符在单个表达式中组合两个 Python 对象。在这种情况下,Python 使用内部的bool() 来判断操作数的真值。此时,两个对象之间的运算,结果将获得一个特定的对象,而不是布尔值。仅当给定操作数显式计算为 True 或 False 时,才会获得 True 或 False

>>> 2 and 3
3
>>> 5 and 0.0
0.0
>>> [] and 3
[]
>>> 0 and {}
0
>>> 1 and {}
{}
>>> False and ""
False
>>> True and ""
''
>>> ["a"] and [1]
[1]

根据以上测试用例结果,当and运算结果为 False 时返回左操作数,当and运算结果为 True 时返回右操作数

关于真值的判断规则,在 python 的文档中有说明

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.

By default, an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object. 1 Here are most of the built-in objects considered false:

  • constants defined to be false: None and False
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

https://docs.python.org/3/library/stdtypes.html#truth-value-testing

以下是对象之间的比较结果集

object1 object2 object1 and object2
False False object1
False True object1
True True object2
True False object2

综上:如果 and 表达式中的操作数是对象而不是布尔表达式,则运算符在计算结果为 False 时返回左侧的对象。否则,它将返回右侧的对象,即使其计算最终结果为 False。

??:如果在做对象逻辑运算,最终结果想得到 bool 类型 True/False的话,可以使用内建函数 bool(expression) 将结果重新实例化一下在返回

5. 高效运算-“快速短路”(short-circuit operator)

短路操作器,听这个名字就知道是快速运算,Python 的逻辑运算符,如 and and or,使用称为短路求值或惰性求值的东西来加速运算,高效得到结果。

例子:为了确认 and 表达式的最终结果,首先计算左操作数,如果它是False,那么整个表达式都是False。在这种情况下,无需计算右侧的操作数,

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python实现京东茅台抢购脚本, 原.. 下一篇Python初步了解装饰器

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目