global param to exec (on win32)
Fredrik Lundh
fredrik at pythonware.com
Sun Jul 28 04:08:54 EDT 2002
david evans wrote:
> # construct "my_dictionary" variable containing outside_func and sdk
> exec "outside_func()", my_dictionary
>
> the call works but the called function complaiss that it can't find
> global 'sdk' and when I print globals() from within the called function,
> its the same as globals() from within the caller, but I expect it to
> be the same as "my_dictionary" within the caller.
>
> Whatever I specify for the "local" param in my exec call, the target sees
> "locals()" as its param list.
>
> Have I misunderstood the way that global/local params to exec work?
yes.
the dictionary you pass to exec is used as the global scope
for that code.
the function itself will use the global scope it was defined in
(python uses lexical scoping).
for details, see
http://www.cwi.nl/www.python.org/doc/current/ref/execframes.html
(the table contains a concise summary)
and
http://www.cwi.nl/www.python.org/doc/current/ref/function.html
(I'm banned from www.python.org, it seems ;-)
</F>
More information about the Python-list
mailing list