How does Python (2.6.4, specifically) determine list membership in general? I’ve run some tests to see what it does:
def main():
obj = fancy_obj(arg='C:\\')
needle = (50, obj)
haystack = [(50, fancy_obj(arg='C:\\')), (1, obj,), needle]
print (1, fancy_obj(arg='C:\\'),) in haystack
print needle in haystack
if __name__ == '__main__':
main()
Which yields:
False
True
This tells me that Python is probably checking the object references, which makes sense. Is there something more definitive I can look at?
From (An Unofficial) Python Reference Wiki:
For the list and tuple types,
x in yis true if and only if there exists an index i such thatx == y[i]is true.So in your example, if the
fancy_objclass stored the value ofargin an instance variable and were to implement an__eq__method that returned True if the twofancy_objsbeing compared had the same value forargthen(1, fancy_obj(arg='C:\\'),) in haystackwould be True.The relevant page of the Standard Library reference is: Built-in Types, specifically 5.6 Sequence Types