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或y
      如果x或y
    • None
      结果永远不会打印在交互式shell中
    (经过一些编辑,这里有一些注释,希望对解决方法/解释有所帮助)

    注1-如何使用not将None转换为False 使用
    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