In a Python expression like str in [str1, str2, str3] or 1 in [1, 2, 3], does the in operator use == or is to compare the first object with the objects in the list?
Does the 'in' operator use "==" or "is" for comparisons?
1.5k Views Asked by Julien Chien At
2
It depends on the object how
inis executed; it is not theinoperator that makes the comparison, it is theobject.__contains__method that does the work.For the Python standard container types (
list,tuple,set,dict, etc.) both identity and equality are used. See the Membership test operations section of the Expressions reference documentation:isis faster to test for, and objects having the same identity implies they are also equal.Custom types are free to implement whatever test they want to do in their
__contains__method.Next, if no
__contains__method is defined, but there is a__iter__method or a__getitem__method, then an iterative search is preformed where the values produced are tested for both identity and equality again. See the above linked documentation for details; the documentation is not that explicit about the identity test but the implementation still uses identity because using it is a performance boost.You can see this when you use
float('nan'), which is never equal to itself or anything else:float('nan')is an exception to the normal expectation that identity implies equality; see What is the rationale for all comparisons returning false for IEEE754 NaN values? for why that is.