Without an eval expression, the arguments must be compile -ed first; a str processed only as an expression, so compile is required for complete statements and arbitrary code.
If you mix it with compile , you can eval to execute arbitrary code, for example:
eval(compile('''def fun(): print 'bbb' ''', '<string>', 'exec'))
This works fine and works the same on Python 2 and Python 3, unlike exec (which is a keyword in Py2 and a function in Py3).
source share