Is there a general way in Python 3 to get a reference to the module's namespace where the function was called? The problem arises from the fact that global
in function references to the namespace where the function was defined, but I want to obtain the reference to a namespace where it was called like in languages with dynamic scope.
In reality, I want to pollute the current namespace with predefined values during an interactive session, the similar to from module import *
but with the call to set_state()
.
from state import set_state
set_state()
The from module import *
syntax is not enough because I have several configurations which are accessed like set_state(3)
. I know that I can pass locals()
as an argument to the function. Nevertheless, I think there exists a more general solution. Also the answer should not be restricted to the current use case.