Checking None With Identity Or Value Comparison
· Def:-
Which is the better way to check None,
with value(==) comparison or identity(is, is not ) comparison.
· Tip:-
Use of identity is better than value operator. As, None the singleton (unique) entity,
it should be compared using identity operator (PEP 8 recommended).
Also, identity comparison is faster than value comparison.
· Code:-
· Value Comparison:- timeit.timeit("None == None", number=100000)
· Identity Comparison :- timeit.timeit("None is None", number=100000)
· Test:-
Identity Comparison
0. 024 0.023 0.023 0.024 0.023 0.023 0.023 0.024 0.026 0.024 0.0237 sec |
Value Comparison
0.033 0.033 0.034 0.033 0.034 0.034 0.034 0.034 0.034 0.034 0.0337 sec |
Result:- Identity Comparison is 30.7% faster.
· Remark :- We can extend the use of Identity comparison to all the data types comparisons(as all data types are unique) in python.
E.g. type('Hello World!!!') is str
#Result True
· Remark :- We can extend the use of Identity comparison to all the data types comparisons(as all data types are unique) in python.
E.g. type('Hello World!!!') is str
#Result True
HTML Comment Box is loading comments...