def f(): exec "import sys" in locals() return lambda x: sys.stdout.write(x) f()("foo\n")