'True' and 'False' in Python

后端 未结 3 876
忘掉有多难
忘掉有多难 2020-12-07 16:13

I tried running this piece of code:

path = \'/bla/bla/bla\'

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

And it prints

3条回答
  •  眼角桃花
    2020-12-07 16:55

    From 6.11. 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). All other values are interpreted as 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.

    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.

提交回复
热议问题