Read any function in runtime

Rhodri James rhodri at wildebst.demon.co.uk
Mon Oct 26 12:15:57 CET 2009


On Fri, 23 Oct 2009 17:39:40 +0100, Matt McCredie <mccredie at gmail.com>  
wrote:

> joao abrantes <senhor.abrantes <at> gmail.com> writes:
>
>>
>> Hey. I want to make a program like this:print "Complete the function
> f(x)="then the user would enter x+2 or 1/x or any other function that  
> only uses
> the variable x. Then my python program would calculate f(x) in some  
> points for
> example in f(2),f(4).. etc . How can I do this?
>>
>
> check out 'eval' or 'exec'.

Then check out all the reasons you shouldn't use them in an
environment that you don't trust absolutely -- if someone wipes
your hard disc, you won't get any sympathy from here.

The safe answer is to write yourself a small parser.  Given that
you've got a very limited symbol set, that shouldn't be too hard.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list