有人刚刚向我展示了这个奇怪的 Python 语法示例。为什么 [4] 有效?
我本来希望它评估为 [5] 或 [6],但两者都不起作用。这里是否有一些不应该的过早优化?
In [1]: s = 'abcd'
In [2]: c = 'b'
In [3]: c in s
Out[3]: True
In [4]: c == c in s
Out[4]: True
In [5]: True in s
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-e00149345694> in <module>()
----> 1 True in s
TypeError: 'in <string>' requires string as left operand, not bool
In [6]: c == True
Out[6]: False