Comparisons to singletons like None should always be done with is
or is not ... beware of writing if x when you really mean
if x is not None e.g. when testing whether a variable or argument that
defaults to None was set to some other value. The other value might
have a type (such as a container) that could be false in a boolean
context!
None
是输入的方式,您也可以接受零等。在我认为你应该使用}更像Python,但我个人认为两者都可以。在
nin is None
而不是nin == None
,很多人会认为{我会做以下事情:
如果你真的想缩短它,你可以这样做,例如:
请注意,这里我使用的是
is
,而不是真实性来测试None
,否则,如果nin = 0
会发生什么?这不是None
,但仍然计算false-y(参见truth value testing),因此style guide的建议是:相关问题 更多 >
编程相关推荐