Evaluating mathematical expressions passed in as strings in python
<p><p> I wish to make a mathematical function ( <code>f(x,y)</code> in this case ) with multiple variables, only two in this case, <code>x</code> and <code>y</code>, which evaluates a mathematical expression which is in a string format initially.
<p>
For example,<br>
If the string is</p>

<pre><code>s = "2*x + sin(y) + x/(y-3.0)"
</code></pre>

<p>The function f(x,y) must be equivalent to</p>

<pre><code>def f(x,y):
    return 2*x + sin(y) + x/(y-3.0)
</code></pre>

<p>The String is constant throughout the program and is initialized at the start.
<bR>The function will be called thousands of times. So I wish it to be very efficient. 
<p>
What is the best way to do so?</p>

<p>Use the <code>eval</code> function
<code>return eval(s)</code></p>

python python-2.7