S
Steven D'Aprano
Suppose I have a function that needs access to globals:
# module A.py
def spam():
g = globals() # this gets globals from A
introspect(g)
As written, spam() only sees its own globals, i.e. those of the module in
which spam is defined. But I want spam to see the globals of the caller.
# module B
import A
A.spam() # I want spam to see globals from B
I can have the caller explicitly pass the globals itself:
def spam(globs=None):
if globs is None:
globs = globals()
introspect(globs)
But since spam is supposed to introspect as much information as possible,
I don't really want to do that. What (if anything) are my other options?
# module A.py
def spam():
g = globals() # this gets globals from A
introspect(g)
As written, spam() only sees its own globals, i.e. those of the module in
which spam is defined. But I want spam to see the globals of the caller.
# module B
import A
A.spam() # I want spam to see globals from B
I can have the caller explicitly pass the globals itself:
def spam(globs=None):
if globs is None:
globs = globals()
introspect(globs)
But since spam is supposed to introspect as much information as possible,
I don't really want to do that. What (if anything) are my other options?