Is it possible to change the behaviour of global and local variables in Python at runtime?
In Python, locals() gives references to variables in the current execution scope, which is a dict object.
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}
Is it possible to replace that reference returned by locals() to a defaultdict, but keep the previous values (a copy of locals()) before replacing it?
I would expect this to avoid UnboundLocalException exceptions when using uninitialized variables and access any variable name in the execution scope (uninitialized variables would take a specified default value).
I've tried to modify the value returned by locals() by reassigning it to locals without success.
The same question goes for globals().