title: Why is __code__ for a function(Python) mutable
body: <p>In a previous question yesterday, in comments, I came to know that in python <code>__code__</code> atrribute of a function is mutable. Hence I can write code as following</p>
  
  <pre><code>def foo():
      print "Hello"
  
  def foo2():
      print "Hello 2"
  
  foo()
  foo.__code__ = foo2.__code__
  foo()
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>Hello
  Hello 2
  </code></pre>
  
  <p>I tried googling, but either because there is no information(I highly doubt this), or the keyword (<code>__code__</code>) is not easily searchable, I couldn't find a use case for this.</p>
  
  <p>It doesn't seem like "because most things in Python are mutable" is a reasonable answer either, because other attributes of functions — <code>__closure__</code> and <code>__globals__</code> — are explicitly read-only (from <a href="https://github.com/python/cpython/blob/master/Objects/funcobject.c#L236">Objects/funcobject.c</a>):</p>
  
  <pre><code>static PyMemberDef func_memberlist[] = {
      {"__closure__",   T_OBJECT,     OFF(func_closure),
       RESTRICTED|READONLY},
      {"__doc__",       T_OBJECT,     OFF(func_doc), PY_WRITE_RESTRICTED},
      {"__globals__",   T_OBJECT,     OFF(func_globals),
       RESTRICTED|READONLY},
      {"__module__",    T_OBJECT,     OFF(func_module), PY_WRITE_RESTRICTED},
      {NULL}  /* Sentinel */
  };
  </code></pre>
  
  <p>Why would <code>__code__</code> be writable while other attributes are read-only?</p>