Alternative to exec
<p>I'm currently trying to code a Python (3.4.4) GUI with tkinter which should allow to fit an arbitrary function to some datapoints. To start easy, I'd like to create some input-function and evaluate it. Later, I would like to plot and fit it using <code>curve_fit</code> from <code>scipy</code>.</p>

<p>In order to do so, I would like to create a dynamic (fitting) function from a user-input-string. I found and read about <code>exec</code>, but people say that (1) it is not safe to use and (2) there is always a better alternative (e.g. <a href="https://stackoverflow.com/questions/33409207/how-to-return-value-from-exec-in-functionand">here</a> and in many other places). So, I was wondering what would be the alternative in this case?</p>

<p>Here is some example code with two nested functions which works but it's not dynamic:</p>

<pre><code>def buttonfit_press():
    def f(x):
         return x+1
    return f 

print(buttonfit_press()(4))
</code></pre>

<p>And here is some code that gives rise to <code>NameError: name 'f' is not defined</code> before I can even start to use xval:</p>

<pre><code>def buttonfit_press2(xval):
    actfitfunc = "f(x)=x+1"
    execstr = "def {}:\n    return {}\n".format(actfitfunc.split("=")[0], actfitfunc.split("=")[1])
    exec(execstr)
    return f

print(buttonfit_press2(4))
</code></pre>

<p>An alternative approach with <code>types.FunctionType</code> discussed here (<a href="https://stackoverflow.com/questions/10303248/true-dynamic-and-anonymous-functions-possible-in-python">10303248</a>) wasn't successful either...</p>

<p>So, my question is: Is there a good alternative I could use for this scenario? Or if not, how can I make the code with <code>exec</code> run?</p>

<p>I hope it's understandable and not too vague. Thanks in advance for your ideas and input.</p>

<hr>

<p>@Gábor Erdős:</p>

<p>Either I don't understand or I disagree. If I code the same segment in the mainloop, it recognizes <code>f</code> and I can execute the code segment from <code>execstr</code>:</p>

<pre><code>actfitfunc = "f(x)=x+1"
execstr = "def {}:\n    return {}\n".format(actfitfunc.split("=")[0], actfitfunc.split("=")[1])
exec(execstr)
print(f(4))
&gt;&gt;&gt; 5
</code></pre>

<hr>

<p>@Łukasz Rogalski:</p>

<p>Printing <code>execstr</code> seems fine to me:</p>

<pre><code>def f(x):
    return x+1
</code></pre>

<p>Indentation error is unlikely due to my editor, but I double-checked - it's fine.
Introducing <code>my_locals</code>, calling it in <code>exec</code> and printing in afterwards shows:</p>

<pre><code>{'f': &lt;function f at 0x000000000348D8C8&gt;}
</code></pre>

<p>However, I still get <code>NameError: name 'f' is not defined</code>.</p>

<hr>

<p>@user3691475:</p>

<p>Your example is very similar to my first example. But this is not "dynamic" in my understanding, i.e. one can not change the output of the function while the code is running.</p>

<hr>

<p>@Dunes:</p>

<p>I think this is going in the right direction, thanks. However, I don't understand yet how I can evaluate and use this function in the next step? What I mean is: in order to be able to fit it, I have to extract fitting variables (i.e. <code>a</code> in <code>f(x)=a*x+b</code>) or evaluate the function at various x-values (i.e. <code>print(f(3.14))</code>).</p>