Edg*_*ian 152 python python-2.7
我对有多少python开发人员if not
在条件语句中使用的方式/原因感到有些困惑.
例如,假设我们有一个功能,
def foo(bar = None):
if not bar:
bar = 2
Run Code Online (Sandbox Code Playgroud)
但为什么要这样呢?我的意思是,不会做if bar != None
或if bar is not None
更明确?什么是if not
尝试说?
小智 192
是的,if bar is not None
更明确,更好,假设它确实是你想要的.情况并非总是如此,存在细微差别:if not bar:
如果bar
是任何零容器或空容器将执行,或者False
.许多人确实使用not bar
他们真正意义上的地方bar is not None
.
归档时间: |
|
查看次数: |
563378 次 |
最近记录: |