Python中"in"的相关性?

Meh*_*dad 107 python syntax python-2.x

我正在制作一个Python解析器,这让我困惑:

>>>  1 in  []  in 'a'
False

>>> (1 in  []) in 'a'
TypeError: 'in <string>' requires string as left operand, not bool

>>>  1 in ([] in 'a')
TypeError: 'in <string>' requires string as left operand, not list
Run Code Online (Sandbox Code Playgroud)

关于结合性等,"in"在Python中的工作原理究竟如何?

为什么这些表达式中没有两个表现方式相同?

Ash*_*ary 123

1 in [] in 'a'被评估为(1 in []) and ([] in 'a').

由于第一个条件(1 in [])是False,整个条件被评估为False; ([] in 'a')从未实际评估,因此不会引发错误.

以下是语句定义:

In [121]: def func():
   .....:     return 1 in [] in 'a'
   .....: 

In [122]: dis.dis(func)
  2           0 LOAD_CONST               1 (1)
              3 BUILD_LIST               0
              6 DUP_TOP             
              7 ROT_THREE           
              8 COMPARE_OP               6 (in)
             11 JUMP_IF_FALSE            8 (to 22)  #if first comparison is wrong 
                                                    #then jump to 22, 
             14 POP_TOP             
             15 LOAD_CONST               2 ('a')
             18 COMPARE_OP               6 (in)     #this is never executed, so no Error
             21 RETURN_VALUE         
        >>   22 ROT_TWO             
             23 POP_TOP             
             24 RETURN_VALUE        

In [150]: def func1():
   .....:     return (1 in  []) in 'a'
   .....: 

In [151]: dis.dis(func1)
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               3 (())
              6 COMPARE_OP               6 (in)   # perform 1 in []
              9 LOAD_CONST               2 ('a')  # now load 'a'
             12 COMPARE_OP               6 (in)   # compare result of (1 in []) with 'a'
                                                  # throws Error coz (False in 'a') is
                                                  # TypeError
             15 RETURN_VALUE   



In [153]: def func2():
   .....:     return 1 in ([] in 'a')
   .....: 

In [154]: dis.dis(func2)
  2           0 LOAD_CONST               1 (1)
              3 BUILD_LIST               0
              6 LOAD_CONST               2 ('a') 
              9 COMPARE_OP               6 (in)  # perform ([] in 'a'), which is 
                                                 # Incorrect, so it throws TypeError
             12 COMPARE_OP               6 (in)  # if no Error then 
                                                 # compare 1 with the result of ([] in 'a')
             15 RETURN_VALUE        
Run Code Online (Sandbox Code Playgroud)

  • @Mehrdad查看与[iPython](http://ipython.org/)一起使用的[Python反汇编程序](http://docs.python.org/library/dis.html)来生成此输出. (6认同)
  • 注意:`[]` 是假的,但`[]` 不是`False` 例如,`[] 和任何东西` 返回`[]`(不是`False`)。 (2认同)

Ale*_*hen 22

Python使用链式比较来做特殊事情.

以下评估方式不同:

x > y > z   # in this case, if x > y evaluates to true, then
            # the value of y is being used to compare, again,
            # to z

(x > y) > z # the parenth form, on the other hand, will first
            # evaluate x > y. And, compare the evaluated result
            # with z, which can be "True > z" or "False > z"
Run Code Online (Sandbox Code Playgroud)

但是,在这两种情况下,如果是第一次比较False,则不会查看语句的其余部分.

对于您的特定情况,

1 in [] in 'a'   # this is false because 1 is not in []

(1 in []) in a   # this gives an error because we are
                 # essentially doing this: False in 'a'

1 in ([] in 'a') # this fails because you cannot do
                 # [] in 'a'
Run Code Online (Sandbox Code Playgroud)

同样为了演示上面的第一条规则,这些是评估为True的语句.

1 in [1,2] in [4,[1,2]] # But "1 in [4,[1,2]]" is False

2 < 4 > 1               # and note "2 < 1" is also not true
Run Code Online (Sandbox Code Playgroud)

python运算符的优先级:http://docs.python.org/reference/expressions.html#summary


pha*_*t0m 11

从文档:

比较可以任意链接,例如,x <y <= z等于x <y和y <= z,除了y仅被评估一次(但在两种情况下,当x <y被发现时,根本不评估z是假的).

这意味着,没有相关性x in y in z!

以下是等效的:

1 in  []  in 'a'
# <=>
middle = []
#            False          not evaluated
result = (1 in middle) and (middle in 'a')


(1 in  []) in 'a'
# <=>
lhs = (1 in []) # False
result = lhs in 'a' # False in 'a' - TypeError


1 in  ([] in 'a')
# <=>
rhs = ([] in 'a') # TypeError
result = 1 in rhs
Run Code Online (Sandbox Code Playgroud)