Python 为什么“not(True) in [False, True]”返回 False?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31421379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:58:28  来源:igfitidea点击:

Why does "not(True) in [False, True]" return False?

pythonpython-2.7python-3.x

提问by Texom512

If I do this:

如果我这样做:

>>> False in [False, True]
True

That returns True. Simply because Falseis in the list.

那返回True。只是因为False在列表中。

But if I do:

但如果我这样做:

>>> not(True) in [False, True]
False

That returns False. Whereas not(True)is equal to False:

那返回False。而not(True)等于False

>>> not(True)
False

Why?

为什么?

采纳答案by Yu Hao

Operator precedence2.x, 3.x. The precedence of notis lower than that of in. So it is equivalent to:

运算符优先级2.x, 3.x。的优先级not低于的优先级in。所以它等价于:

>>> not ((True) in [False, True])
False

This is what you want:

这就是你想要的:

>>> (not True) in [False, True]
True


As @Ben points out: It's recommended to never write not(True), prefer not True. The former makes it look like a function call, while notis an operator, not a function.

正如@Ben 指出的那样:建议永远不要写not(True),更喜欢not True. 前者使它看起来像一个函数调用,而它not是一个运算符,而不是一个函数。

回答by mooiamaduck

Operator precedence. inbinds more tightly than not, so your expression is equivalent to not((True) in [False, True]).

运算符优先级。in绑定比 更紧密not,因此您的表达式等效于not((True) in [False, True])

回答by alfasin

It's all about operator precedence(inis stronger than not). But it can be easily corrected by adding parentheses at the right place:

这完全是关于运算符优先级in比 强not)。但是可以通过在正确的位置添加括号来轻松更正:

(not(True)) in [False, True]  # prints true

writing:

写作:

not(True) in [False, True]

is the same like:

是一样的:

not((True) in [False, True])

which looks if Trueis in the list and returns the "not" of the result.

它查看是否True在列表中并返回结果的“不是”。

回答by user3636636

It is evaluating as not True in [False, True], which returns Falsebecause Trueis in [False, True]

它正在评估 as not True in [False, True],它返回,False因为True[False, True]

If you try

如果你试试

>>>(not(True)) in [False, True]
True

You get the expected result.

你得到了预期的结果。

回答by Roshan Mathews

not x in yis evaluated as x not in y

not x in y被评估为 x not in y

You can see exactly what's happening by disassembling the code. The first case works as you expect:

通过反汇编代码,您可以准确地看到发生了什么。第一种情况按您的预期工作:

>>> x = lambda: False in [False, True]
>>> dis.dis(x)
  1           0 LOAD_GLOBAL              0 (False)
              3 LOAD_GLOBAL              0 (False)
              6 LOAD_GLOBAL              1 (True)
              9 BUILD_LIST               2
             12 COMPARE_OP               6 (in)
             15 RETURN_VALUE

The second case, evaluates to True not in [False, True], which is Falseclearly:

第二种情况,评估为True not in [False, True],这False显然是:

>>> x = lambda: not(True) in [False, True]
>>> dis.dis(x)
  1           0 LOAD_GLOBAL              0 (True)
              3 LOAD_GLOBAL              1 (False)
              6 LOAD_GLOBAL              0 (True)
              9 BUILD_LIST               2
             12 COMPARE_OP               7 (not in)
             15 RETURN_VALUE        
>>> 

What you wanted to express instead was (not(True)) in [False, True], which as expected is True, and you can see why:

您想要表达的是(not(True)) in [False, True],正如预期的那样True,您可以看到原因:

>>> x = lambda: (not(True)) in [False, True]
>>> dis.dis(x)
  1           0 LOAD_GLOBAL              0 (True)
              3 UNARY_NOT           
              4 LOAD_GLOBAL              1 (False)
              7 LOAD_GLOBAL              0 (True)
             10 BUILD_LIST               2
             13 COMPARE_OP               6 (in)
             16 RETURN_VALUE        

回答by Nick Louloudakis

Let's see it as a collection containment checking operation: [False, True]is a list containing some elements.

让我们把它看作一个集合包含检查操作:[False, True]是一个包含一些元素的列表。

The expression True in [False, True]returns True, as Trueis an element contained in the list.

表达式True in [False, True]返回True,作为True列表中包含的元素。

Therefore, not True in [False, True]gives the "boolean opposite", notresult of the above expression (without any parentheses to preserve precedence, as inhas greater precedence than notoperator). Therefore, not Truewill result False.

因此,not True in [False, True]给出not上述表达式的结果“布尔相反” (没有任何括号以保持优先级,因为in其优先级高于not运算符)。因此,not True将导致False

On the other hand, (not True) in [False, True], is equal to False in [False, True], which is True(Falseis contained in the list).

另一方面,(not True) in [False, True], 等于False in [False, True],即True(False包含在列表中)。

回答by Kasramvd

Alongside the other answers that mentioned the precedence of notis lower than in, actually your statement is equivalent to :

除了提到优先级not低于的其他答案之外in,实际上您的陈述等效于:

not (True in [False, True])

But note that if you don't separate your condition from the other ones, python will use 2 roles (precedenceor chaining) in order to separate that, and in this case python used precedence. Also, note that if you want to separate a condition you need to put all the condition in parenthesis not just the object or value :

但请注意,如果您不将您的条件与其他条件分开,python 将使用 2 个角色 ( precedenceor chaining) 将其分开,在这种情况下,python 使用优先级。另外,请注意,如果要分隔条件,则需要将所有条件放在括号中,而不仅仅是对象或值:

(not True) in [False, True]


But as mentioned, there is another modification by python on operators that is chaining:

但如前所述,python 对链接的运算符进行了另一个修改:

Based on python documentation:

基于python文档

Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chainingfeature as described in the Comparisons section.

请注意,比较、成员资格测试和身份测试都具有相同的优先级,并且具有如比较部分所述的从左到右的链接功能。

For example the result of following statement is False:

例如以下语句的结果是False

>>> True == False in [False, True]
False

Because python will chain the statements like following :

因为python会链接如下语句:

(True == False) and (False in [False, True])

Which exactly is False and Truethat is False.

这到底是False and True那是False

You can assume that the central object will be shared between 2 operations and other objects (False in this case).

您可以假设中心对象将在 2 个操作和其他对象之间共享(在这种情况下为 False)。

And note that its also true for all Comparisons, including membership tests and identity tests operations which are following operands :

请注意,它也适用于所有比较,包括成员测试和身份测试操作,这些操作遵循操作数:

in, not in, is, is not, <, <=, >, >=, !=, ==

Example :

例子 :

>>> 1 in [1,2] == True
False

Another famous example is number range :

另一个著名的例子是 number range :

7<x<20

which is equal to :

这等于:

7<x and x<20   

回答by asmeurer

To clarify on some of the other answers, adding parentheses aftera unary operator does not change its precedence. not(True)does not make notbind more tightly to True. It's just a redundant set of parentheses around True. It's much the same as (True) in [True, False]. The parentheses don't do anything. If you want the binding to be more tight, you have to put the parentheses around the whole expression, meaning both the operator and the operand, i.e., (not True) in [True, False].

为了澄清其他一些答案,一元运算符之后添加括号不会改变其优先级。not(True)不会使not绑定更紧密True。它只是True.周围的一组冗余括号。它与(True) in [True, False]. 括号没有任何作用。如果您希望绑定更紧密,则必须在整个表达式周围加上括号,这意味着运算符和操作数,即(not True) in [True, False]

To see this another way, consider

要以另一种方式看待这一点,请考虑

>>> -2**2
-4

**binds more tightly than -, which is why you get the negative of two squared, not the square of negative two (which would be positive four).

**绑定比 更紧密-,这就是为什么你得到二平方的负数,而不是负二的平方(这将是正四)。

What if you did want the square of negative two? Obviously, you'd add parentheses:

如果你确实想要负二的平方怎么办?显然,您需要添加括号:

>>> (-2)**2
4

However, it's not reasonable to expect the following to give 4

但是,期望以下内容给出是不合理的 4

>>> -(2)**2
-4

because -(2)is the same as -2. The parentheses do absolutely nothing. not(True)is exactly the same.

因为-(2)-2. 括号完全没有作用。not(True)完全一样。