>>> "f" in "foo"
True
>>> "f" in "foo" == True
False
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么第二个表达式是 False。我看到==
优先级高于in
. 但随后我希望得到一个异常,这就是当我添加括号时会发生的情况:
>>> "f" in ("foo" == True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
Run Code Online (Sandbox Code Playgroud)
foo
似乎只有当位于 两侧时,表达式才为 True ==
,如下所示:
>>> "f" in "foo" == "foo"
True
>>> "f" in "foo" == "bar"
False
Run Code Online (Sandbox Code Playgroud)
我缺少什么?Python 在这里实际计算什么?