Ptyhon 中使用 is None 而不是 ==None 问题
首先需要区分 Python 中is
和==
的区别:
- python - Is there a difference between "==" and "is"? - StackOverflow (opens new window)
- python - Is there any difference between "foo is None" and "foo == None"? - StackOverflow (opens new window)
is
比较在两个对象是否相同时使用(即通过比较在内存中的标识符(id(obj)
)),而==
通过调用__eq__()
方法比较两个的值是否相等。
看下面的 回答 (opens new window) :
is
is generally preferred when comparing arbitrary objects to singletons likeNone
because it is faster and more predictable.is
always compares by object identity, whereas what==
will do depends on the exact type of the operands and even on their ordering.This recommendation is supported by PEP 8, which explicitly states that "comparisons to singletons like None should always be done with is or is not, never the equality operators."
通常,在将任意对象与None
之类的单个对象进行比较时首选is
,因为它更快且更可预测。 is
总是根据对象标识进行比较,而==
的作用取决于运算对象的确切类型,甚至取决于它们的顺序。
PEP 8 支持此建议,该声明明确指出“与单例的比较(如None
,应该始终使用is
或 not
进行比较,永远不要使用相等运算符进行比较)”。
在工作效率上,is
的效率明显高于==
:
>>> a = timeit.timeit("1 is None", number=10000000) # 0.6208912429997326
>>> b = timeit.timeit("1 == None", number=10000000) # 0.9341017190004095
>>> a /b
0.6125248705781231
2
3
4
5
6
当然,在数值上来看差距不是很大,但是量变引起质变。
# 参考链接
Python: "is None" vs "==None" | Jared Grubb (opens new window)
需要注意的是:除了None
之外,别的代码的地方最好都不要使用is
比较!