title: Why can a Python dict have multiple keys with the same hash?
body: <p>I am trying to understand the Python <code>hash</code> function under the hood. I created a custom class where all instances return the same hash value. </p>
  
  <pre><code>class C:
      def __hash__(self):
          return 42
  </code></pre>
  
  <p>I just assumed that only one instance of the above class can be in a <code>dict</code> at any time, but in fact a <code>dict</code> can have multiple elements with the same hash.</p>
  
  <pre><code>c, d = C(), C()
  x = {c: 'c', d: 'd'}
  print(x)
  # {&lt;__main__.C object at 0x7f0824087b80&gt;: 'c', &lt;__main__.C object at 0x7f0823ae2d60&gt;: 'd'}
  # note that the dict has 2 elements
  </code></pre>
  
  <p>I experimented a little more and found that if I override the <code>__eq__</code> method such that all the instances of the class compare equal, then the <code>dict</code> only allows one instance.</p>
  
  <pre><code>class D:
      def __hash__(self):
          return 42
      def __eq__(self, other):
          return True
  
  p, q = D(), D()
  y = {p: 'p', q: 'q'}
  print(y)
  # {&lt;__main__.D object at 0x7f0823a9af40&gt;: 'q'}
  # note that the dict only has 1 element
  </code></pre>
  
  <p>So I am curious to know how a <code>dict</code> can have multiple elements with the same hash.</p>