import os def foo(s): f = lambda a: a + "; print '%s'" % os.getcwd() exec(f(s)) foo("print 'asdf'")
What error does this give? Why?
This one is pretty tough. I'll put a simpler illustration that gives the same error in the comments as a hint.
(Updated to fix inadvertent SyntaxError in the exec'd string, pointed out by Ian Bicking. Python bails with the error I intended before reaching that point, though.)