Fal*_*rri 6 python syntax logic
除非我疯了if None not in x并且if not None in x是等同的.有首选版本吗?我想None not in更多的是英语,因此更加pythonic,但not None in更像是其他语言语法.有首选版本吗?
Mar*_*ers 14
它们编译为相同的字节码,所以是的,它们是等价的.
>>> import dis
>>> dis.dis(lambda: None not in x)
  1           0 LOAD_CONST               0 (None)
              3 LOAD_GLOBAL              1 (x)
              6 COMPARE_OP               7 (not in)
              9 RETURN_VALUE
>>> dis.dis(lambda: not None in x)
  1           0 LOAD_CONST               0 (None)
              3 LOAD_GLOBAL              1 (x)
              6 COMPARE_OP               7 (not in)
              9 RETURN_VALUE
该文件还明确指出,两者是等价的:
x not in s回归否定x in s.
如你所说,None not in x更自然的英语,所以我更喜欢使用它.
如果你写not y in x,可能不清楚你是否意味着not (y in x)或(not y) in x.如果你使用,没有歧义not in.
表达方式
not (None in x) 
(为清晰起见而添加了parens)是一个普通的布尔否定.然而,
None not in x
为更易读的代码添加了特殊语法(这里没有可能,也没有意义,在in前面使用和,等等).如果添加了这种特殊情况,请使用它.
同样适用于
foo is not None
与
not foo is None
我发现阅读"不是"更清楚.作为额外的奖励,如果表达式是更大的布尔表达式的一部分,则not的范围立即清楚.