title: Python: dynamically create function at runtime
body: <p>How to dynamically create a function in Python?</p>
  
  <p>I saw a few answers here but I couldn't find one which would describe the most general case.</p>
  
  <p>Consider:</p>
  
  <pre><code>def a(x):
      return x + 1
  </code></pre>
  
  <p>How to create such function on-the-fly? Do I have to <code>compile('...', 'name', 'exec')</code> it? But what then? Creating a dummy function and replacing its code object for then one from the compile step?</p>
  
  <p>Or should I use <code>types.FunctionType</code>? How?</p>
  
  <p>I would like to customize everything: number of argument, their content, code in function body, the result, ...</p>