深入理解 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。在这种情况下,无需计算右侧的操作数,就已经知道最终结果了

>>> def true_func():
...     print("Running true_func()")
...     return True
...

>>> def false_func():
...     print("Running false_func()")
...     return False
...

>>> true_func() and false_func()  # Case 1
Running true_func()
Running false_func()
False

>>> false_func() and true_func()  # Case 2
Running false_func()
False

>>> false_func() and false_func()  # Case 3
Running false_func()
False

>>> true_func() and true_func()  # Case 4
Running true_func()
Running true_func()
True

短路操作器,是提升代码性能的一种有效手段。在逻辑运算表达式时,可考虑如下两个原则:

  1. 将耗时的表达式放在 and 关键字的右侧。这样,如果短路规则生效,则不会运行代价高昂的表达式。
  2. 将更有可能为 false 的表达式放在 and 关键字的左侧。这样, 更有可能通过仅计算左操作数来确定整个表达式是否为 false。

当然,如果一定要执行运算符两边的表达式,则可以使用位运算符号 (&, |, ~),替代逻辑运算符

>>> def true_func():
...     print("Running true_func()")
...     return True
...

>>> def false_func():
...     print("Running false_func()")
...     return False
...

>>> # Use logical and
>>> false_func() and true_func()
Running false_func()
False

>>> # Use bitwise and
>>> false_func() & true_func()
Running false_func()
Running true_func()
False

6. bool 逻辑运算的万能公式

Operation Result Notes
法则 1 x or y if x is true, then x, else y 1
法则 2 x and y if x is false, then x, else y 2
法则 3 not x if x is false, then True, else False 3

Notes:

  1. This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
  2. This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
  3. not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.

多复杂的组合表达式,最终都可以一重重拆解成一个个独立的小单元,在做合并

就拿开头的引子来说

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

拆成三步来看

  1. b and c --> 运用法则 2 (if x is false, then x, else y) 所以结果是 c 即 False
  2. b and a --> a 运用法则 2 (if x is false, then x, else y) 所以结果是 a 即 ["a", "b"]
  3. False or ["a", "b"] 运用法则 1 (if x is true, then x, else y) 所以结果是 ["a", "b"]

7. 参考

  1. python-and-operator
  2. python-docs-truth-value-testing
  3. Python3 源码阅读 数据类型-Int.md
posted @ 2023-09-08 12:48  JonPan  阅读(115)  评论(0编辑  收藏  举报