A problem with exec statement

TPJ tprimke at interia.pl
Fri Apr 14 05:54:51 EDT 2006


> Use the exec statement without the in-clause to get the desired effect:
>
> >>> def f():
> ...     a = "a"
> ...     exec "a = 'B'"
> ...     print a
> ...
> >>> f()
> B
> <snip>

Well... I *do* realize that. But this is *not* my problem. I have a
function with another nested one. If I used "exec ..." instead of "exec
... in some_dict, some_dict" I would get the "SyntaxError: unqualified
exec is not allowed in function 'f' it contains a nested function with
free variables".

To be honest, the message cited above is the answer to the question
"Why have I put those globals(), locals() in the exec statments?".




More information about the Python-list mailing list