Best practice for Python: assert command() == False [closed]

北慕城南 提交于 2019-12-10 23:28:08

问题


I wonder what is better/best:

>>> def command():
...     return False
...
>>> assert command() == False
>>> assert command() is False
>>> assert not command()

Cheers, Markus


回答1:


The most pythonic is the third. It is equivalent to:

assert bool(command()) != False



回答2:


Coding conventions can be studied here: PEP 8 Style Guide for Python Code

There you will find:

Don't compare boolean values to True or False using ==

Yes:   if greeting:
No:    if greeting == True:
Worse: if greeting is True:


来源:https://stackoverflow.com/questions/14733883/best-practice-for-python-assert-command-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!