None It's a singleton object , all None The memory addresses of are the same , So use is Judgment is ok .
If you use ==
Judgment may be problematic , because ==
Called __eq__()
Methods may be overloaded , The result is not as expected :
class ReloadEq():
def __eq__(self, second_value):
return True
req = ReloadEq()
print(req == None)
Output :
True
besides ,is The time efficiency is also better than == Higher :
import timeit
print(timeit.timeit('None is None',number=10**7))
print(timeit.timeit('None == None',number=10**7))
Output :
0.1993637309933547
0.2477139010006795
because is Just compare the addresses , and == Need to compare values , And it may involve objects that need to be traversed, such as lists , You need to compare each element .
Reference link :https://blog.csdn.net/qq_34152244/article/details/100639985