Is it safe to modify the output of globals()? Is it safe to modify the output of globals()? python python

Is it safe to modify the output of globals()?


Modifying locals() doesn't work reliably, even in CPython. It happens to work in module and class scopes, but it fails inside a function (any modifications "won't take", since locals() provides a copy of the local namespace in that case, rather than a reference to the real thing)

However, globals() is different, since that always refers to the module namespace, and module namespaces are required to behave like ordinary dictionaries. So yes, the lack of a warning on globals() isn't an oversight, it really is allowed.