首页 > 编程语言 >深入理解 Python and 逻辑运算符(踩坑)

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

时间:2023-09-08 12:55:18浏览次数:38  
标签:逻辑 False Python true 运算符 bool func false True

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

标签:逻辑,False,Python,true,运算符,bool,func,false,True
From: https://www.cnblogs.com/panlq/p/17687294.html

相关文章

  • python模拟用户登录
    python模拟用户登录目录python模拟用户登录一、授权认证二、Cookie认证一、授权认证1、HTTP基础认证importrequestsfromrequests.authimportHTTPBasicAuthurl="https://xxx.xxx.xxx/"username="admin"password="admin"#HTTP基础认证response=requests.ge......
  • python中元组的解包用法,以及 *在方法中的使用方法
    下面是关于python中*的用法详解1.*做法乘法运算符在python中,*作为乘法运算符是使用,用来进行两个数的乘法运算。示例代码:a=3b=4c=a*bprint(c)#12 2.*作为函数的可变参数(是元组的形式导入)单星号(*):*args。将所有参数以元组(tuple)的形式导入双星号(**):**kwargs。将参......
  • 【Python&GIS】矢量数据投影转换(坐标转换)
    ​    之前跟大家分享过面矢量数据投影转换和点矢量数据投影转换,但博主在日常工作的过程中发现之前分享的面矢量数据投影转换有时候会出现错误,或者转换后的效果不好。再一次偶然的过程中发现了新的坐标转换(投影转换)函数,所以今天和大家分享一下,大家在进行坐标转换时可以......
  • 【Python&RS】基于GDAL修改栅格数据的DN值
    ​    遥感工作者离不开栅格数据,有时候我们可能需要修改栅格数据的值,但ENVI和ArcGIS中并没有直接修改DN值的工具,只有栅格计算器、Bandmath这些工具去计算整个波段的值,或者EditClassificationImage工具可以修改ENVI分类后的像元值,但这个工具只对分类格式有效,博主整不......
  • 【题解】《PTA-Python程序设计》题目集分享
    第1章-1从键盘输入两个数,求它们的和并输出(30分)本题目要求读入2个整数A和B,然后输出它们的和。输入格式:在一行中给出一个被加数在另一行中给出一个加数输出格式:在一行中输出和值。输入样例:在这里给出一组输入。例如:18-48输出样例:在这里给出相应的输出。例如:......
  • Python学习日记 京东工单信息获取
    importrequestsimportcsvimportrandomf=open('vc.csv',mode='a',encoding='utf-8',newline='')csv_writer=csv.DictWriter(f,fieldnames=['客户姓名','订单编号','pin'])csv_wri......
  • Python FastAPI 异步获取 Neo4j 数据
    前提条件先往Neo4j里,准备数据参考:https://www.cnblogs.com/vipsoft/p/17631347.html#创建传承人搭建FastAPI项目:https://www.cnblogs.com/vipsoft/p/17684079.html改造utils/neo4j_provider.py增加了暴露给外面调用的属性,同时提供了同步和异步执行的驱动#!/usr/bin/py......
  • Python实操:内存管理与优化策略
    在Python开发过程中,合理有效地管理和优化内存使用是提高程序性能和效率的关键。本文将深入探讨Python中的内存管理机制,并分享一些实用的优化策略和具体操作步骤,帮助您更好地利用资源、减少内存占用并提升代码执行速度。一、了解Python的垃圾回收机制垃圾回收是自动处理不再被......
  • python3中几乎所有的内置函数以及简述
    以下是Python3中的所有内置函数以及它们的简单中文描述:abs(x):返回x的绝对值。all(iterable):如果可迭代对象中的所有元素都为True,则返回True;否则返回False。any(iterable):如果可迭代对象中的任何一个元素为True,则返回True;否则返回False。ascii(object):返回一个可打印的字符串,其中非......
  • Linux系统上安装.tar.gz格式的Python源码包
    要在Linux系统上安装.tar.gz格式的Python包,您可以按照以下步骤进行操作:解压文件:使用以下命令将.tar.gz文件解压缩:tar-zxvfpackage.tar.gz这将在当前目录下创建一个包含源代码的新文件夹。进入源代码目录:使用cd命令进入解压后的源代码目录:cdpackage检查依赖库:执行以下命令检查......