How can I (easily) take a string such as "sin(x)*x^2"
which might be entered by a user at runtime and produce a Python function that could be evaluated for any value of x
?
Python's own internal compiler can parse this, if you use Python notation.
If your change the notation slightly, you'll be happier.
import compiler eq= "sin(x)*x**2" ast= compiler.parse( eq )
You get an abstract syntax tree that you can work with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With