title: Using __contains__ on a list of custom class objects
body: <p>I have a simple class defined like this:</p>
  
  <pre><code>class User(object):
      def __init__(self, id=None, name=None):
          self.id = id
          self.name = name
  
      def __contains__(self, item):
          return item == self.id
  </code></pre>
  
  <p>Using this class, I can do simple checks of single instances of the class like so:</p>
  
  <pre><code>&gt;&gt;&gt; user1 = User(1, 'name_1')
  &gt;&gt;&gt; 1 in user1
  True
  &gt;&gt;&gt; 2 in user1
  False
  </code></pre>
  
  <p>This is working as expected. </p>
  
  <p>How can I check if a value is in a list of <code>User</code> objects though? It always seems to return False.</p>
  
  <p>Example:</p>
  
  <pre><code>from random import randint
  from pprint import pprint
  users = [User(x, 'name_{}'.format(x)) for x in xrange(5)]
  pprint(users, indent=4)
  
  for x in xrange(5):
      i = randint(2,6) 
      if i in users:
          print("User already exists: {}".format(i))
      else:
          print("{} is not in users list. Creating new user with id: {}".format(i, i))
          users.append(User(i, 'new_user{}'.format(i)))
  pprint(users, indent=4)
  </code></pre>
  
  <p>This creates output similar to this:</p>
  
  <pre><code>[   0 =&gt; name_0, 
      1 =&gt; name_1, 
      2 =&gt; name_2, 
      3 =&gt; name_3, 
      4 =&gt; name_4]
  6 is not in users list. Creating new user with id: 6
  6 is not in users list. Creating new user with id: 6
  6 is not in users list. Creating new user with id: 6
  3 is not in users list. Creating new user with id: 3
  3 is not in users list. Creating new user with id: 3
  [   0 =&gt; name_0,
      1 =&gt; name_1,
      2 =&gt; name_2,
      3 =&gt; name_3,
      4 =&gt; name_4,
      6 =&gt; new_user6,
      6 =&gt; new_user6,
      6 =&gt; new_user6,
      3 =&gt; new_user3,
      3 =&gt; new_user3]
  </code></pre>
  
  <p>The issue is that the user with id <code>6</code> should have only been created 1 time, because it wasn't created already. The second and third time that <code>6</code> was attempted, it should have failed. The user would id <code>3</code> shouldn't have been recreated at all because it was part of the initialization of the <code>users</code> variable.</p>
  
  <p>How do I modify by <code>__contains__</code> method to be able to properly utilize <code>in</code> when comparing against multiple instances of my class?</p>