这个表达式是如何计算的?“foo”中的“f”==真

2024-04-26 05:09:38 发布

您现在位置:Python中文网/ 问答频道 /正文

>>> "f" in "foo"
True
>>> "f" in "foo" == True
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

似乎只有当foo位于==的两侧时,表达式才是真的,如下所示:

>>> "f" in "foo" == "foo"
True
>>> "f" in "foo" == "bar"
False

我错过了什么?Python在这里实际计算的是什么


Tags: infalsetruemostfoo表达式stdin情况