Best practice for Python: assert command() == False
我想知道什么是更好/更好:
1 2 3 4 5 6 | >>> def command(): ... return False ... >>> assert command() == False >>> assert command() is False >>> assert not command() |
干杯,马库斯
可以在这里研究编码约定:
PEP 8 Python代码样式指南
在那里你会发现:
Don't compare boolean values to True or False using ==
1 2 3 | Yes: if greeting: No: if greeting == True: Worse: if greeting is True: |
最pythonic是第三个。 它相当于:
1 | assert bool(command()) != False |