exec and globals and locals ...

dieter dieter at handshake.de
Thu Sep 19 01:04:27 EDT 2019


Eko palypse <ekopalypse at gmail.com> writes:
> Why does f1 work? I've expected an exception as no global dict has been provided
> ...
>def f1(...):
>  exec("...")
>...

The documentation ("https://docs.python.org/3/library/functions.html#exec")
tells you:

exec(object[, globals[, locals]])
...
In all cases, if the optional parts are omitted, the code is executed in the current scope. ...


You can see from it that "globals" is optional.
And that, if "globals" is missing, then
"exec" is executed in the current scope ("f1" in your case).




More information about the Python-list mailing list