【发布时间】:2018-10-10 17:24:21
【问题描述】:
在 Python3 中,PEP8 中的Truth Value Testing 表示对于if A is None:,它可以转换为if not A:。像这样,既然下面的代码看起来如此凌乱且难以立即掌握,是否可以用一种或另一种方式更简洁地表达它?
if A is not None and B is not None and C is not None:
【问题讨论】:
-
你的逻辑颠倒了。
if A is None可以有时被if not A替换,if A is not None可以有时被替换为if A,这取决于A的可能值。因此,根据上下文将if语句替换为if A and B and C可能是合适的。 -
if A:和if A is not None:在语义上不等效。我很确定 PEP8 确实没有这么说,因为这不是风格问题。 -
A = 0-- >if A:不会进入 if 块。if A is not None:将进入 if 块 -
我编辑了你指出的逻辑!谢谢!
标签: python python-3.x if-statement pep8