if not start:
new.next = None
return new
“如果不是”是什么意思?该代码什么时候执行?
是说start == None:然后做点什么吗?
if
是声明。not start
是表达式,not
是布尔运算符。
not
返回True
如果操作数(start
在这里)被认为是假的。Python认为所有对象都是真实的,除非它们是数字零,空容器,None
对象或布尔False
值。如果为true,则not
返回。请参阅文档中的“真值测试”部分。False
start
因此,如果start
是None
,那么确实not start
将是正确的。start
也可以是0
,或为空列表,字符串,元组字典或集合。许多自定义类型还可以指定它们等于数字0或应视为空:
>>> not None
True
>>> not ''
True
>>> not {}
True
>>> not []
True
>>> not 0
True
注意:因为None
是单例(在Python进程中只有该对象的一个副本),所以应始终使用is
或进行测试is not
。如果您严格要测试tat start
is None
,请使用:
if start is None:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句