Python最佳实践:assert command() == False
我想知道哪种方式更好或者最好:
>>> def command():
... return False
...
>>> assert command() == False
>>> assert command() is False
>>> assert not command()
谢谢,Markus
2 个回答
10
你可以在这里了解编码规范: PEP 8 Python代码风格指南
在这里你会发现:
不要用 == 来比较布尔值是否为 True 或 False
Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:
5
最符合Python风格的是第三种写法。它和下面的代码是一样的:
assert bool(command()) != False