how can I execute a function string

Premshree Pillai premshree_python at yahoo.co.in
Tue Jan 13 06:38:14 EST 2004


You need to use eval() to run code on-the-fly. See
http://www.python.org/doc/current/lib/built-in-funcs.html

 --- Rajarshi Guha <rajarshi at presidency.com> wrote: >
Hi ,
>  I have some code that generates a function on the
> fly in a string. 
> At a later point in time I want to execute this
> function (which also
> requires a parameters to be passed to it). So the
> code is something like
> this:
> 
> def generate_func():
> 	s = """ def function(x):
> 	print x
> 	return 2
> """
> 	return s
> 
> funcstring = generate_func()
> retval = ....
> 
> That is, retval should have the value returned from
> the evaluation of the
> function in the string funcstring.
> 
> Is this possible by means of simple function calls
> or does this involve
> some sort of black magic?
> 
> Thanks,
> -- 
> http://mail.python.org/mailman/listinfo/python-list 

________________________________________________________________________
Yahoo! India Mobile: Download the latest polyphonic ringtones.
Go to http://in.mobile.yahoo.com




More information about the Python-list mailing list