翼度科技»论坛 编程开发 python 查看内容

深入理解 Python and 逻辑运算符(踩坑)

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18


1. 引子
  1. def enabled() -> bool:
  2.     a = ["a,"b"]
  3.         b = True
  4.     c = False
  5.     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都表示相同的字典键
  1. >>> True == 1 == 1.0
  2. True
  3. >>> (hash(True), hash(1), hash(1.0))
  4. (1, 1, 1)
  5. >>> issubclass(bool, int)
  6. True
  7. >>> isinstance(True, int)
  8. True
  9. >>> isinstance(False, int)
  10. True
  11. >>> int(True)
  12. 1
  13. >>> int(False)
  14. 0
  15. >>> help(bool)
  16. Help on class bool in module builtins:
  17. class bool(int)
  18.     |  bool(x) -> bool
  19.     |
  20.     |  Returns True when the argument x is true, False otherwise.
  21.         |  The builtins True and False are the only two instances of the class bool.
  22.         |  The class bool is a subclass of the class int, and cannot be subclassed.
复制代码
3. bool类型之间的 and 运算

and 运算符有两个操作数,可以是 bool,object,或一个组合
  1. >>> True and True
  2. True
  3. >>> False and False
  4. False
  5. >>> True and False
  6. False
  7. >>> False and True
  8. False
复制代码
以上示例表明,仅当表达式中的两个操作数都为 true 时,和表达式才返回 True。由于 and 运算符需要两个操作数来构建表达式,因此它是一个二元运算符。
当两边操作书都为 bool 类型时,二元操作运算结果如下。
operand1operand2operand1 and operand2TrueTrueTrueTrueFalseFalseFalseFalseFalseFalseTrueFalse当然,以上的结果也适用于两边操作数一个 bool 表达式的情况。
expression1 and expression2
  1. >>> 5>3 and 5==3+2
  2. True
复制代码
4. 不同类型之间的 and 运算

可以使用 and 运算符在单个表达式中组合两个 Python 对象。在这种情况下,Python 使用内部的bool() 来判断操作数的真值。此时,两个对象之间的运算,结果将获得一个特定的对象,而不是布尔值。仅当给定操作数显式计算为 True 或 False 时,才会获得 True 或 False
  1. >>> 2 and 3
  2. 3
  3. >>> 5 and 0.0
  4. 0.0
  5. >>> [] and 3
  6. []
  7. >>> 0 and {}
  8. 0
  9. >>> 1 and {}
  10. {}
  11. >>> False and ""
  12. False
  13. >>> True and ""
  14. ''
  15. >>> ["a"] and [1]
  16. [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
以下是对象之间的比较结果集
object1object2object1 and object2FalseFalseobject1FalseTrueobject1TrueTrueobject2TrueFalseobject2综上:如果 and 表达式中的操作数是对象而不是布尔表达式,则运算符在计算结果为 False 时返回左侧的对象。否则,它将返回右侧的对象,即使其计算最终结果为 False。
⚠️:如果在做对象逻辑运算,最终结果想得到 bool 类型 True/False的话,可以使用内建函数 bool(expression) 将结果重新实例化一下在返回
5. 高效运算-“快速短路”(short-circuit operator)

短路操作器,听这个名字就知道是快速运算,Python 的逻辑运算符,如 and and or,使用称为短路求值或惰性求值的东西来加速运算,高效得到结果。
例子:为了确认 and 表达式的最终结果,首先计算左操作数,如果它是False,那么整个表达式都是False。在这种情况下,无需计算右侧的操作数,就已经知道最终结果了
  1. >>> def true_func():
  2. ...     print("Running true_func()")
  3. ...     return True
  4. ...
  5. >>> def false_func():
  6. ...     print("Running false_func()")
  7. ...     return False
  8. ...
  9. >>> true_func() and false_func()  # Case 1
  10. Running true_func()
  11. Running false_func()
  12. False
  13. >>> false_func() and true_func()  # Case 2
  14. Running false_func()
  15. False
  16. >>> false_func() and false_func()  # Case 3
  17. Running false_func()
  18. False
  19. >>> true_func() and true_func()  # Case 4
  20. Running true_func()
  21. Running true_func()
  22. True
复制代码
短路操作器,是提升代码性能的一种有效手段。在逻辑运算表达式时,可考虑如下两个原则:

  • 将耗时的表达式放在 and 关键字的右侧。这样,如果短路规则生效,则不会运行代价高昂的表达式。
  • 将更有可能为 false 的表达式放在 and 关键字的左侧。这样, 更有可能通过仅计算左操作数来确定整个表达式是否为 false。
当然,如果一定要执行运算符两边的表达式,则可以使用位运算符号 (&, |, ~),替代逻辑运算符
  1. >>> def true_func():
  2. ...     print("Running true_func()")
  3. ...     return True
  4. ...
  5. >>> def false_func():
  6. ...     print("Running false_func()")
  7. ...     return False
  8. ...
  9. >>> # Use logical and
  10. >>> false_func() and true_func()
  11. Running false_func()
  12. False
  13. >>> # Use bitwise and
  14. >>> false_func() & true_func()
  15. Running false_func()
  16. Running true_func()
  17. False
复制代码
6. bool 逻辑运算的万能公式

OperationResultNotes法则 1x or yif x is true, then x, else y1法则 2x and yif x is false, then x, else y2法则 3not xif x is false, then True, else False3Notes:

  • This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
  • This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
  • 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.
多复杂的组合表达式,最终都可以一重重拆解成一个个独立的小单元,在做合并
就拿开头的引子来说
  1. def enabled() -> bool:
  2.     a = ["a,"b"]
  3.         b = True
  4.     c = False
  5.     return (b and c) or (b and a)
复制代码
拆成三步来看

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


来源:https://www.cnblogs.com/panlq/p/17687294.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具