osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

exec and globals and locals ...


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).