Python-带分组和不带分组的逻辑求值顺序
有人能解释一下“假”优于“无”或“无”优于“假”的行为吗Python-带分组和不带分组的逻辑求值顺序,python,Python,有人能解释一下“假”优于“无”或“无”优于“假”的行为吗 1 in None or [] Traceback (most recent call last): File "C:\ProgramData\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code exec(code_obj, self.user_global_ns, self.user_ns) Fi
1 in None or []
Traceback (most recent call last):
File "C:\ProgramData\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-0f48647a1d09>", line 1, in <module>
1 in None or []
TypeError: argument of type 'NoneType' is not iterable
1 in (None or [])
Out[5]: False
1 in (None or [1])
Out[6]: True
1 in [1] or None
Out[15]: True
1 in []
Out[17]: False
Below one returns None
1 in [] or None
**Returns None **
1无或[]
回溯(最近一次呼叫最后一次):
文件“C:\ProgramData\Anaconda2\lib\site packages\IPython\core\interactiveshell.py”,第2881行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”,第1行,在
1无或[]
TypeError:类型为“NoneType”的参数不可编辑
1英寸(无或[])
Out[5]:假
1英寸(无或[1])
Out[6]:对
[1]中的1或无
Out[15]:对
1英寸[]
Out[17]:假
低于1则返回无
1英寸[]或无
**返回无**
为什么下面返回异常,但上面没有返回异常
1 in None
Traceback (most recent call last):
File "C:\ProgramData\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-21-71e8d29ac0d2>", line 1, in <module>
1 in None
TypeError: argument of type 'NoneType' is not iterable
1无
回溯(最近一次呼叫最后一次):
文件“C:\ProgramData\Anaconda2\lib\site packages\IPython\core\interactiveshell.py”,第2881行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”,第1行,在
1比1
TypeError:类型为“NoneType”的参数不可编辑
您的代码从左到右求值。所以[]中的1为False。因此,在False
和None
之间,您会得到None
>>> False or None
>>> False or False
False
>>> None or False
False
也许更清楚一点。这就是它的评估方法
1英寸[]或无
步骤1:计算[]中的1,因此我们得到False
步骤2:比较False
和None
。由于第一个元素为False,我们只得到第二个值。您的代码从左到右求值。所以[]
中的1为False。因此,在False
和None
之间,您会得到None
>>> False or None
>>> False or False
False
>>> None or False
False
也许更清楚一点。这就是它的评估方法
1英寸[]或无
步骤1:计算[]中的1,因此我们得到False
步骤2:比较False
和None
。因为第一个元素为False,所以我们只得到第二个值。打印结果以查看它:
>>> print(1 in [] or None)
None
- None不是iterable,在操作符中的右侧需要一个iterable,这就是您得到该错误的原因
TypeError:类型为“NoneType”的参数不可编辑
空列表是一个可编辑的。iterable是一个能够一次返回一个成员的对象
- 假不比无好,也不比假好。这是因为or运算符就是这样工作的
>>> False or 5
5
所以
打印结果以查看它:
>>> print(1 in [] or None)
None
- None不是iterable,在操作符中的右侧需要一个iterable,这就是您得到该错误的原因
TypeError:类型为“NoneType”的参数不可编辑
空列表是一个可编辑的。iterable是一个能够一次返回一个成员的对象
- 假不比无好,也不比假好。这是因为or运算符就是这样工作的
>>> False or 5
5
所以
让我们依次看看其中的每一项:
1 in None or [] -> (1 in None) or [] -> (Error)
1 in (None or []) -> 1 in [] -> False
1 in (None or [1]) -> 1 in [1] -> True
1 in [1] or None -> True or None -> True
1 in [] -> False
1 in [] or None -> False or None -> None (not printed)
有几个关键点需要理解:
- 评估顺序是从左到右
y中的x
优先于y或z
x或y
如果x或y
None
结果永远不会打印在交互式shell中让我们依次看看其中的每一个:
1 in None or [] -> (1 in None) or [] -> (Error)
1 in (None or []) -> 1 in [] -> False
1 in (None or [1]) -> 1 in [1] -> True
1 in [1] or None -> True or None -> True
1 in [] -> False
1 in [] or None -> False or None -> None (not printed)
有几个关键点需要理解:
- 评估顺序是从左到右
优先于y中的x
y或z
如果x或yx或y
结果永远不会打印在交互式shell中None
not
运算符将None
转换为布尔值True
。同样,使用notnot
将None
转换为布尔值False
In [4]: a = 1 in [] or None
In [5]: print(a)
None
In [6]: print(not a)
True
In [7]: print(not not a)
False
注2-如何使用bool()将None转换为False
根据下面的评论(作者@tobias_k),我们实际上可以用较短的代码将None
直接转换为False
:
In [8]: print(bool(None))
False
注3-in运算符仅适用于iterable数据类型
中的运算符仅适用于可编辑的数据结构。我们可以使用hasattr()
函数检查数据结构是否是可移植的
例如,列表
、元组
和范围
都是可编辑的:
In [31]: hasattr([], '__iter__')
Out[31]: True
In [32]: hasattr((), '__iter__')
Out[32]: True
In [33]: hasattr(range(0), '__iter__')
Out[33]: True
In [40]: hasattr(None, '__iter__')
Out[40]: False
In [41]: hasattr(1, '__iter__')
Out[41]: False
In [42]: hasattr(True, '__iter__')
Out[42]: False
In [43]: hasattr(False, '__iter__')
Out[43]: False
由于这些是可编辑的,因此
操作符中的可以工作(即不抛出错误):
诸如None
、int
、bool
等数据类型不可编辑:
In [31]: hasattr([], '__iter__')
Out[31]: True
In [32]: hasattr((), '__iter__')
Out[32]: True
In [33]: hasattr(range(0), '__iter__')
Out[33]: True
In [40]: hasattr(None, '__iter__')
Out[40]: False
In [41]: hasattr(1, '__iter__')
Out[41]: False
In [42]: hasattr(True, '__iter__')
Out[42]: False
In [43]: hasattr(False, '__iter__')
Out[43]: False
因此,
运算符中的会对这些不可编辑的类型抛出一个错误。e、 g
In [44]: 1 in None
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-44-71e8d29ac0d2> in <module>()
----> 1 1 in None
TypeError: argument of type 'NoneType' is not iterable
In [45]: 1 in 1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-36200f7947c7> in <module>()
----> 1 1 in 1
TypeError: argument of type 'int' is not iterable
In [47]: 1 in False
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-47-0ba098e6e3c7> in <module>()
----> 1 1 in False
TypeError: argument of type 'bool' is not iterable
In [48]: 1 in True
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-48-5e7fb522bdca> in <module>()
----> 1 1 in True
TypeError: argument of type 'bool' is not iterable
[44]中的:无中的1
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1比1
TypeError:类型为“NoneType”的参数不可编辑
In[45]:1/1
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1比1
TypeError:类型为“int”的参数不可编辑
In[47]:1在False中
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1错误
TypeError:类型为“bool”的参数不可编辑
In[48]:1 In True
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->真的吗
TypeError:类型为“bool”的参数不可编辑
注4-理解“或”运算符
见h