Overriding a class to be left hand side of in operator
<p>Apparently the <code>__contains__</code> special method allows to implement the <code>in</code> evaulation when the object with the method is on the right hand side of it. I have a piece of code where the <code>in</code> must be implemented by the left hand operand. How do I go about it?</p>

<p>I was able to get this to work by overriding eq (how do you put underscores in the text?).</p>

<p>Consider the code "value in some_list". It will iterate through some_list until it finds value. But how does it know if it found value? By comparing it. That's what you want to override.</p>

<pre><code>class Twenty(object):
    def __init__(self):
        self.x = 20

    def __eq__(self, y):
        print "comparing", self.x, "to", y
        return self.x == y

value = Twenty()
assert value in [10, 20, 30]
assert value not in [1, 2, 3]
</code></pre>

<p>output</p>

<pre><code>comparing 20 to 10
comparing 20 to 20
comparing 20 to 1
comparing 20 to 2
comparing 20 to 3
</code></pre>

python contains