title: How does python compute the hash of a tuple
body: <p>In python, if I have a tuple with many elements, is its hash calculated from its elements' <code>id</code>s or its elements' content? </p>
  
  <p>In this example,</p>
  
  <pre><code>a = (1, [1,2])
  hash(a)
  </code></pre>
  
  <p>It errors out saying list is unhashable. So I guess it's not computed by id, or probably there is a check on whether the element is mutable.</p>
  
  <p>Now see this example</p>
  
  <pre><code>class A: pass
  a0 = A()
  ta = (1, a0)
  hash(ta)  # -1122968024
  a0.x = 20
  hash(ta)  # -1122968024
  </code></pre>
  
  <p>Here it turns out the hash of <code>ta</code> does not change with the modification of its element, i.e., <code>a0</code>. So maybe <code>a0</code>'s id is used for the hash calculation? Is <code>a0</code> somehow considered as immutable? How does python know if a type is mutable?</p>
  
  <p>Now consider this case</p>
  
  <pre><code>b = (1, 2)
  id(b)  # 3980742764
  c = (1, 2)
  id(c)  # 3980732588
  tb = (1, b)
  tc = (1, c) 
  hash(tb)  # -1383040070
  hash(tc)  # -1383040070
  </code></pre>
  
  <p>It seems the content of <code>b</code> and <code>c</code> are used for the hash calculation.</p>
  
  <p>How should I understand these examples?</p>