Explaining exec(globals, separate_locals)

Dieter Maurer dieter at handshake.de
Tue Sep 21 14:00:25 EDT 2021


Terry Reedy wrote at 2021-9-20 12:46 -0400:
>The second paragraph of the current exec entry
>https://docs.python.org/3.11/library/functions.html#exec
>ends with a sentence I wrote.
>
>"If exec gets two separate objects as globals and locals, the code will
>be executed as if it were embedded in a class definition."
>
>Would the following would be clearer?
>
>"If exec gets two separate objects as globals and locals, the code will
>be executed in two namespaces, the same as is done with the suite of a
>class statements."

It is better but the reference to class statements does not help
(at least me) very much.

Instead, I would try to explain how those namespaces are used,
e.g. bindings are stored in the "local" namespace; lookups
are first performed in the "local", then the "global" namespace,
etc. (e.g. about the effect of `global`, `nonlocal`).



--
Dieter


More information about the Python-list mailing list