5

风格指南的最后一点http://www.python.org/dev/peps/pep-0008

读...

不要使用 == 将布尔值与 True 或 False 进行比较。

为什么?

编辑只是为了弄清楚我在问什么(这表明问题本身),当你写的时候

if something:
    print "something is true"

您正在对布尔值进行隐式转换,这可能会或可能不会起作用,具体取决于 true 的含义。恕我直言,不鼓励这种形式的编程,因为它可能导致副作用。

numberOfApples = -1
if numberOfApples:
    print "you have apples" # is not what is intended.

if numberOfApples == True:
    print "you have apples" # is also not what is intended.

iHaveApples = numberOfApples > 0
if iHaveApples is True:    # Edit:  corrected this.. the "is" is better than the ==
  print  "you have apples" # is correct.

隐式转换掩盖了逻辑错误。那么为什么风格指南鼓励这样做呢?

4

4 回答 4

7

这意味着你应该写

if greeting:

代替:

if greeting == True:

同样,您也不应该这样写:

if (greeting == True) == True:

额外的测试是多余的,不会为代码添加任何价值,因此应该删除它们。

于 2012-04-19T06:11:24.577 回答
2

恕我直言,风格指南的重点是以一种有意义的方式标准化一些常见的结构,因此您最终不会得到最终做同样事情的完全不同的陈述。此外,不寻常的形式可能表明程序员有某种理由以不同的方式做事,也许他试图实现与语句看起来不同的东西。

如果要测试语句的真假,只需使用语句本身或在其前面加上not. 如果您必须确保该语句的计算结果为Trueor False(不仅仅是一个真/假值),您可以使用statement is True- 尽管样式指南不鼓励它 - 或者检查它的类型(使用isinstance)。但这通常是糟糕的设计,除非你有充分的理由这样做,否则你应该避免这样做。

statement == True由于许多原因,使用是危险的:1) 仅使用True,使用其他“真实”值(如[1])失败;2) 如果语句返回的值重新定义,可能会产生意想不到的结果__eq__;3) 如果参数的顺序发生变化等,可能会产生不同的结果。请注意,如果值实现__nonzero__or ,仅使用语句也可能返回不同的真/假值__len__,但对于常规使用,这通常不是问题。

一些例子表明如果你偏离了风格,事情会变得多么糟糕:

if True: print True # True
if 1: print True    # True
if [1]: print True  # True

True is True # True
1 is True    # False
[1] is True  # False

True == True # True
1 == True    # True
[1] == True  # False

编辑:更多:

if 1: print True # True
if 2: print True # True

1 == True # True
2 == True # False

1 is True # False
2 is True # False

更新:正如@Marcin 指出的那样,您可以使用bool将值强制为True/ False,保证只有这些值会出现。该函数的结果与值的默认真/假一致(因此__nonzero____len__考虑在内)。一些例子:

if bool(1): print True # True
bool(1) == True        # True
bool(1) is True        # True

if bool(2): print True # True
bool(2) == True        # True
bool(2) is True        # True

1 or 2              # 1
1 and 2             # 2
bool(1) or bool(2)  # True
bool(1) and bool(2) # True

bool(1) == bool(2)  # True
bool(1) is bool(2)  # True
于 2012-04-19T06:31:30.267 回答
1

因为它是多余的。

if hasChildren:

是相同的

if hasChildren == True:

但更简洁,更容易阅读。

于 2012-04-19T06:11:32.960 回答
0

排序:

# A, Good:
if numberOfApples > 0:
    print "you have apples"

# B, Also good:
iHaveApples = numberOfApples > 0
if iHaveApples:
  print  "you have apples"

# C, Bad:
iHaveApples = numberOfApples > 0
if iHaveApples == True:
  print  "you have apples"

为什么你会选择 C ​​而不是 A 或 B?

更新:

我认为您在某些极端情况下大汗淋漓,但如果这些极端情况在您的项目中很重要,请使用适当的比较。一般来说,我们确实对 的类型有所了解iHaveApples,例如,我们知道它是使用 比较的结果>。我认为在代码中使用该信息是合理且良好的做法。如果您要问,“如果我认为它是一个 bool 并且结果是一个 int 或其他东西怎么办?” 我会说你的代码中有一个错误,你应该找到它,修复它,并编写一个测试,以防你再次犯同样的错误。不要依赖 python 在运行时发现你的错误。

我会断言,并留给你来证明你是否愿意,它的if iHaveApples:行为完全相同,但运行得更快,因为if iHaveApples is True:你确定这iHaveApples是一个布尔值。最后,我将举例说明何时is会导致不良行为,至少在我看来。

>>> import numpy
>>> totalApples = numpy.sum([1, 2, 3]) == 6
>>> totalApples
True
>>> totalApples is True
False

如果你愿意,我会让你弄清楚为什么这不起作用(提示,检查type(totalApples))。

于 2012-04-23T06:26:45.370 回答