i am having a problem with scope. i would like to have a function operate on the result of the builtin function dir(). so, as a simple example, i want to
>>> print (dir())
>>> .... bunch of stuff ...
but, i want define a function which operates on this results of dir(). however, if try this like so
>>> def print_dir(dummy_var=None):
print dir()
>>> print_dir()
>>> ['dummy_var']
i cant access the namespace of what called print_dir which i understand is a scope thing and makes sense, but is there a way to access the namespace from which a function is called? or do i have to pass the result of dir() to the function? like.
>>> act_on_namespace(dir())
the specific goal in mind is something like this,
def act_on_namespace()
for k in dir():
try:
print(eval(k).__module__)
except:
pass
No, there is no straightforward way to manipulate the calling namespace like that. The simplest way is to do what you already suggested: call
act_on_namespace(dir()). If you really want to get crazy, you could look at theinspectmodule, which allows you to look through the call stack, but that is probably not a good idea unless what you’re doing is writing some kind of debugging tool.