Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是如何运作的?文档在哪里?
a = 0 or "" or None or 2 print(a) a = 2 or "" or None or 0 print(a)
将or运算符视为一个函数:
or
or(x, y): if x: return x else: return y
然后a = 0 or ""将与 相同a = or(0, "")。由于0评估为False,这将返回""。
a = 0 or ""
a = or(0, "")
0
False
""
现在,a = 0 or "" or None or 2本质上是
a = 0 or "" or None or 2
a = or(or(or(0, ""), None) 2)
最里面的or将返回"",下一个将意识到""仍然评估为 False 并返回None,而最外面的将最终返回2。
None
2