Python saving an eval function
问题 Say I have a function fun(f, x, y) where x and y are numbers and f is a string specifying a function such as "1 / x ** 2 + y" . I wish to use this function f a lot, say a few million times, and the values of x and y change between each use. Therefore calling eval(f) takes a significant amount of time as opposed to just calculating the value of the function each time. (About 50x, in my measured case.) Is there any way to save this function f so that I would only have to call eval once? PS.