> If the user must be able to enter in the function, then it would be
better
> to evaluate this once and turn it into some sort of function that
you can
> call inside the loop (it's the eval that is so expensive).  How to
do that
> depends a lot on how complex the possible functions can be (if
they'll only
> include 'x*+/-' and numbers, for example, it's not so tricky).

exp = raw_input('Type expression')
func = eval('lambda x: " + exp)

print func(42)


etc...

Or if you really can't grokm lambda:

exec('def func(x): return " + exp)

should do the same...

Alan G.

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to