[Discuss] Compiling/running user-supplied Python code
Brian Quinlan
brian at sweetapp.com
Thu Aug 10 12:04:53 PDT 2006
noel at natnix.com wrote:
> Looks like python's eval() doesn't like "def foo():" statements.
eval() doesn't like ANY statements. eval() only works with expressions.
> I got
> it to work with eval(compile()) though:
>
> defstr = "def foo(): print('foo!')"
> eval(compile(defstr, '<defstr>', 'exec'))
>
> evalstr = "foo()"
> eval(evalstr)
Instead of munging your statements into an expression, you can just use
exec, which already works with statements.
Cheers,
Brian
More information about the Discuss
mailing list