title: Why and how are Python functions hashable?
body: <p>I recently tried the following commands in Python:</p>
  
  <pre><code>&gt;&gt;&gt; {lambda x: 1: 'a'}
  {&lt;function __main__.&lt;lambda&gt;&gt;: 'a'}
  
  &gt;&gt;&gt; def p(x): return 1
  &gt;&gt;&gt; {p: 'a'}
  {&lt;function __main__.p&gt;: 'a'}
  </code></pre>
  
  <p>The success of both <code>dict</code> creations indicates that both lambda and regular functions are hashable. (Something like <code>{[]: 'a'}</code> fails with <code>TypeError: unhashable type: 'list'</code>).</p>
  
  <p>The hash is apparently not necessarily the ID of the function:</p>
  
  <pre><code>&gt;&gt;&gt; m = lambda x: 1
  &gt;&gt;&gt; id(m)
  140643045241584
  &gt;&gt;&gt; hash(m)
  8790190327599
  &gt;&gt;&gt; m.__hash__()
  8790190327599
  </code></pre>
  
  <p>The last command shows that the <code>__hash__</code> method is explicitly defined for <code>lambda</code>s, i.e., this is not some automagical thing Python computes based on the type.</p>
  
  <p>What is the motivation behind making functions hashable? For a bonus, what is the hash of a function?</p>