Python 假或无与无或假
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3914667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
False or None vs. None or False
提问by Virgiliu
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.
这种行为让我感到困惑。有人可以向我解释为什么会这样吗?我希望他们都表现得一样。
采纳答案by RichieHindle
The expression x or yevaluates to xif xis true, or yif xis false.
表达式的x or y计算结果为xifx为真,或yifx为假。
Note that "true" and "false" in the above sentence are talking about "truthiness", not the fixed values Trueand False. Something that is "true" makes an ifstatement succeed; something that's "false" makes it fail. "false" values include False, None, 0and [](an empty list).
请注意,上句中的“true”和“false”是在谈论“真实性”,而不是固定值True和False。“真实”的东西使if陈述成功;“错误”的东西使它失败。“假”值包括False,None,0和[](空单)。
回答by sth
From a boolean point of view they both behave the same, both return a value that evaluates to false.
从布尔的角度来看,它们的行为相同,都返回一个评估为 false 的值。
orjust "reuses" the values that it is given, returning the left one if that was true and the right one otherwise.
or只是“重用”给定的值,如果为真则返回左边的值,否则返回右边的值。
回答by Andrey Gubarev
Condition1 or Condition2
if Condition1 is False then evalute and return Condition2. None evalutes to False.
如果 Condition1 为 False,则评估并返回 Condition2。None 评估为 False。
回答by martineau
The "or" operator returns the valueof its first operand, if that value is true in the Pythonic boolean sense (aka its "truthiness"), otherwise it returns the valueof its second operand, whatever it happens to be. See the subsection titled Boolean operationsin the section on Expressionsin the current online documentation.
“或”运算符返回其第一个操作数的值,如果该值在 Pythonic 布尔意义上为真(也就是它的“真实性”),否则它返回其第二个操作数的值,无论它是什么。见标题为第布尔运算中的部分表达了在目前的在线文档。
In both your examples, the first operand is considered false, so the value of the second one becomes the result of evaluating the expression.
在您的两个示例中,第一个操作数都被视为 false,因此第二个操作数的值成为评估表达式的结果。
回答by Nick T
A closely related topic: Python's orand andshort-circuit. In a logical oroperation, if any argument is true, then the whole thing will be true and nothing else needs to be evaluated; Python promptly returns that "true" value. If it finishes and nothing was true, it returns the last argument it handled, which will be a "false" value.
一个密切相关的主题:Python's orand andshort-circuit。在逻辑or运算中,如果任何参数为真,则整个事物都为真,不需要评估其他任何参数;Python 立即返回该“真”值。如果它完成并且没有任何内容为真,它会返回它处理的最后一个参数,这将是一个“假”值。
andis the opposite, if it sees any false values, it will promptly exit with that "false" value, or if it gets through it all, returns the final "true" value.
and恰恰相反,如果它看到任何假值,它会立即退出该“假”值,或者如果它全部通过,则返回最终的“真”值。
>>> 1 or 2 # first value TRUE, second value doesn't matter
1
>>> 1 and 2 # first value TRUE, second value might matter
2
>>> 0 or 0.0 # first value FALSE, second value might matter
0.0
>>> 0 and 0.0 # first value FALSE, second value doesn't matter
0
回答by Bo?tjan Mejak
You must realize that None, Falseand Trueare all singletons.
你必须意识到None,False并且True都是单身人士。
For example, if foo is not Nonemeans that foohas some value other than None. This works the same as just having if foowhich is basically if foo == True.
例如,if foo is not None意味着foo具有除 之外的某些值None。这与只拥有if foowhich 基本相同if foo == True。
So, not Noneand Truework the same way. Also, Noneand Falsework the same way.
因此,not None与True工作方式相同。此外,None与False工作方式相同。
>>> foo = not None
>>> bool(foo)
True
>>> foo = 5 # Giving an arbitrary value here
>>> bool(foo)
True
>>> foo = None
>>> bool(foo)
False
>>> foo = 5 # Giving an arbitrary value here
>>> bool(foo)
True
The important thing to realize and to be aware of when coding is that when comparing two things, Noneneeds is, but Trueand Falseneed ==. Avoid if foo == Noneand do only if foo is Noneand avoid if foo != Noneand do only if foo is not None. In the case of if foo is not None, simply do if foo. In the case of if foo is None, simply do if not foo.
编码时要意识到和意识到的重要事情是比较两件事时,None需要is,但是True和False需要==。避免if foo == None只做if foo is None,避免if foo != None只做if foo is not None。在的情况下if foo is not None,干脆做if foo。在的情况下if foo is None,干脆做if not foo。
Note: Trueis basically 1and Falseis basically 0. In the old days of Python, we had only 1for a value of true and we had 0for a value of false. It's more understandable and human-friendly to say Trueinstead of 1and more understandable and human-friendly to say Falseinstead of 0.
注:True基本上1和False基本0。在 Python 的旧时代,我们只有1一个值 true 和0一个 false 值。说True而不是说1更容易理解和人性化,False而不是说更容易理解和人性化0。

