Creating function object from text

Peter Hansen peter at engcorp.com
Sat Apr 17 12:39:22 EDT 2004


Edward C. Jones wrote:

> Suppose I have a piece of text that defines a function:
> 
> text ="""\
> def fun(i):
>     return i + 2
> """
> 
> How do I turn this into a function object, f, which can be called or 
> passed around?

exec text

If you need to do this other than in the global namespace of the
module, read up on the exec statement and how you can pass in a
dictionary of your own.

-Peter



More information about the Python-list mailing list