Is there a way to write a Python script that creates and executes code?
<p>Is there a way in Python to create Python code inside the Python script and then execute/test it?</p>

<p>My function has the following type of form (as an example)</p>

<pre><code>def f(n):
    if n&lt;=3: return [0, 0, 6, 12][n]
    return 2*f(n-1) - 4*f(n-2) - 5*f(n-3) + 15*f(n-4)
</code></pre>

<p>But I want to be able to create these kinds of functions dynamically (or any arbitrary function for that matter) and then test their outputs during runtime (as opposed to copying/pasting this function into the script and then manually testing it). </p>

<p>Not sure if this makes sense, please ask for elaboration if needed. I've already looked into eval and exec but couldn't get them to work with entire function definitions, just basic statements like 1+2, etc.</p>
