False或None与None或False

False或None与None或False

本文介绍了False或None与None或False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In [20]: print None or False
-------> print(None or False)
False

In [21]: print False or None
-------> print(False or None)
None

这种行为使我感到困惑.有人可以向我解释为什么会这样吗?我希望他们的行为都一样.

This behaviour confuses me. Could someone explain to me why is this happening like this? I expected them to both behave the same.

推荐答案

如果x为true,则表达式x or y的值为x;如果x为false,则为y.

The expression x or y evaluates to x if x is true, or y if x is false.

请注意,以上句子中的"true"和"false"是指真实性",而不是固定值TrueFalse.如果是"true",则使if语句成功.某些假"的东西会使它失败. "false"值包括FalseNone0[](空列表).

Note that "true" and "false" in the above sentence are talking about "truthiness", not the fixed values True and False. Something that is "true" makes an if statement succeed; something that's "false" makes it fail. "false" values include False, None, 0 and [] (an empty list).

这篇关于False或None与None或False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:41