bundles / IPython 9.18.0.dev / IPython / terminal / embed / _EmbedGlobals
class
IPython.terminal.embed:_EmbedGlobals
Signature
class _EmbedGlobals(module_dict, local_ns)Members
Summary
Globals namespace for an embedded shell.
Extended Summary
Code typed in an embedded shell is compiled as module-level code, so any new nested scope it creates (a lambda, a generator expression, a comprehension, a function body...) looks its free variables up in globals(), not in the local namespace the shell was embedded in. With a plain module __dict__ as globals this makes the caller's local variables invisible to those scopes (gh-136)
def f(): x = 1 embed() # then type: (lambda: x)() -> NameError
This dict subclass keeps the interpreter's normal, C-level storage as a snapshot of the caller module's globals, but resolves reads through the caller's local namespace first, mimicking the closure lookup the code would have had if it were written in place. The interpreter only honors this __getitem__ override because the shell passes a dict subclass to exec, which disables the exact-dict fast path of LOAD_GLOBAL.
STORE_GLOBAL/DELETE_GLOBAL bypass __setitem__ overrides and mutate the C-level storage directly, so sync_to_module propagates those (rare) mutations back to the real module on exit.
Aliases
IPython.terminal.embed._EmbedGlobals