简体   繁体   English

Python中的'True'和'False'

[英]'True' and 'False' in Python

I tried running this piece of code: 我尝试运行这段代码:

path = '/bla/bla/bla'

if path is True:
    print "True"
else:
    print "False"

And it prints False . 它打印错误 I thought Python treats anything with value as True . 我认为Python将任何有价值的东西视为 Why is this happening? 为什么会这样?

is compares identity. is比较认同。 A string will never be identical to a not-string. 字符串永远不会与非字符串相同。

== is equality. ==是平等。 But a string will never be equal to either True or False . 但是字符串永远不会等于TrueFalse

You want neither. 你也不想要。

path = '/bla/bla/bla'

if path:
    print "True"
else:
    print "False"

From http://docs.python.org/reference/expressions.html#boolean-operations : 来自http://docs.python.org/reference/expressions.html#boolean-operations

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). 在布尔运算的上下文中,以及控制流语句使用表达式时,以下值被解释为false:False,None,所有类型的数字零,以及空字符串和容器(包括字符串,元组,列表,字典) ,集和frozensets)。 All other values are interpreted as true. 所有其他值都被解释为true。

The key phrasing here that I think you are misunderstanding is "interpreted as false" or "interpreted as true". 我觉得你误解的关键措辞是“解释为假”或“解释为真”。 This does not mean that any of those values are identical to True or False, or even equal to True or False. 这并不意味着这些值中的任何一个都与True或False相同,甚至不等于True或False。

The expression '/bla/bla/bla' will be treated as true where a Boolean expression is expected (like in an if statement), but the expressions '/bla/bla/bla' is True and '/bla/bla/bla' == True will evaluate to False for the reasons in Ignacio's answer. 表达式'/bla/bla/bla'将被视为true,其中布尔表达式是预期的(如在if语句中),但表达式'/bla/bla/bla' is True'/bla/bla/bla' == True将在Ignacio的答案中评估为False。

While the other posters addressed why is True does what it does, I wanted to respond to this part of your post: 虽然其他海报解释了为什么is True会做它的作用,但我想回复你帖子的这一部分:

I thought Python treats anything with value as True. 我认为Python将任何有价值的东西视为真。 Why is this happening? 为什么会这样?

Coming from Java, I got tripped up by this, too. 来自Java,我也被这个绊倒了。 Python does not treat anything with a value as True . Python不会将值为True任何内容视为对象。 Witness: 见证人:

if 0:
    print("Won't get here")

This will print nothing because 0 is treated as False . 这将不会打印,因为0被视为False In fact, zero of any numeric type evaluates to False . 实际上,任何数字类型的零都会计算为False They also made decimal work the way you'd expect: 他们还按照您的预期进行decimal

from decimal import *
from fractions import *

if 0 or 0.0 or 0j or Decimal(0) or Fraction(0, 1):
    print("Won't get here")

Here are the other value which evaluate to False : 以下是评估为False的其他值:

if None or False or '' or () or [] or {} or set() or range(0):
    print("Won't get here")

Sources: 资料来源:

  1. https://medium.com/@dawran6/python-truth-value-testing-is-awesome-dae6c23cc1c2 https://medium.com/@dawran6/python-truth-value-testing-is-awesome-dae6c23cc1c2
  2. https://docs.python.org/3.7/library/stdtypes.html#truth-value-testing https://docs.python.org/3.7/library/stdtypes.html#truth-value-testing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM