newbie: generate a function based on an expression

Mike Meyer mwm at
Tue Dec 13 23:08:23 CET 2005

"Jacob Rael" <jacob.rael at> writes:
> In CppSim, classes are defined that allow various functions to be
> defined, like amplifiers. In some cases they are linear:
> y = A*x
> some have offsets:
> y = A*x + off
> some are non-linear
> y = A*x - C*x**3
> The coefficients and the function will remain constant once the
> function is defined.

It appears that CppSim is a language, including parsers, evaluators,
and the other things that a language processor usually has. To do a
proper port of it, you'll have to recreate all that mechanism.

On the other hand, if you want CppSim-like program with pythonesque
expressions written in Python, then eval is probably the best way to
go - if you can live with the security concerns. An example of this is
my P(x) package, as it relates to the original F(x) package. P(x)
programs have all the power of Python, and so deserve to be treated
with the same suspicion you'd treat a Python program.

> I read about the security concerns involved in using eval(). I don't
> expect this project to grow to the point where I require a web
> interface. However, since I am learning, I might as well learn the
> right way.

Web interfaces are just one source of suspect data. That's the common
one, because that allows arbitrary users to submit data. But it's not
uncommon to not trust data from other sources as well.

In this case, you really want to let users write general-purpose
functions. The non-eval way to do that is to use closures and higher
order functions, but that's not going to look much like cppSim.

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list