Creating function object from text

Peter Hansen peter at
Sat Apr 17 18:39:22 CEST 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.


More information about the Python-list mailing list