how can I execute a function string

Rajarshi Guha rajarshi at
Tue Jan 13 02:17:28 CET 2004

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

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?


More information about the Python-list mailing list