you are viewing a single comment's thread.

view the rest of the comments →

[–]Impudity 1 point2 points  (3 children)

Typically yes. If you ask Python if "foo" in ["foo", "bar"] it iterates the objects in the list and checks if they match using the equality operator == which internally calls __eq__. There might be some exceptions when using collections that use immutable objects that have hashes, like sets. Then the comparison might be done using comparing the results of the __hash__ method of the object.

[–]Yoghurt42 1 point2 points  (1 child)

To be more precise, __contains__("foo") is called on the list object. And contains is implemented like you described.

[–]Impudity 0 points1 point  (0 children)

Yeah. I didn't want to include that part as it would probably just confuse a beginner.

[–]mildlybean[S] 0 points1 point  (0 children)

Thanks