title: Can I create function using 'function' class?
body: <p>I would like to know, just for fun, if I can create functions using <em>function</em> class constructor, i.e. without language construct <em>def</em>, just like creating class by instantiating type object. I know, function constructor takes 2 args - code object and globals. But I don't know how I should <strong>compile</strong> the source properly.</p>
  
  <pre><code>&gt;&gt;&gt; def f(): 
  ...     pass
  
  &gt;&gt;&gt; Function = type(f) 
  &gt;&gt;&gt; Function
  &lt;class 'function'&gt;
  &gt;&gt;&gt; code = compile("x + 10", "&lt;string&gt;", "exec")
  &gt;&gt;&gt; f = Function(code, globals())
  &gt;&gt;&gt; f()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;string&gt;", line 1, in &lt;module&gt;
  NameError: name 'x' is not defined
  &gt;&gt;&gt; f(20)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: &lt;module&gt;() takes 0 positional arguments but 1 was given
  </code></pre>