Does this do what you intended?
d = dict(globals())
d.update(locals())
If I read the documentation correctly, you create a copy of the globals()
dict, then you overwrite any duplicates and insert new entries from the locals()
dict (since the locals()
should have preference within your scope, anyway).
I haven't had any luck in getting a proper function to return the full dictionary of variables in scope of the calling function. Here's the code (I only used pprint to format the output nicely for SO):
from pprint import *
def allvars_bad():
fake_temp_var = 1
d = dict(globals())
d.update(locals())
return d
def foo_bad():
x = 5
return allvars_bad()
def foo_good():
x = 5
fake_temp_var = "good"
d = dict(globals())
d.update(locals())
return d
pprint (foo_bad(), width=50)
pprint (foo_good(), width=50)
and the output:
{'PrettyPrinter': <class pprint.PrettyPrinter at 0xb7d316ec>,
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': 'temp.py',
'__name__': '__main__',
'__package__': None,
'allvars_bad': <function allvars_bad at 0xb7d32b1c>,
'd': <Recursion on dict with id=3084093748>,
'fake_temp_var': 1,
'foo_bad': <function foo_bad at 0xb7d329cc>,
'foo_good': <function foo_good at 0xb7d32f0c>,
'isreadable': <function isreadable at 0xb7d32c34>,
'isrecursive': <function isrecursive at 0xb7d32c6c>,
'pformat': <function pformat at 0xb7d32bc4>,
'pprint': <function pprint at 0xb7d32b8c>,
'saferepr': <function saferepr at 0xb7d32bfc>}
{'PrettyPrinter': <class pprint.PrettyPrinter at 0xb7d316ec>,
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': 'temp.py',
'__name__': '__main__',
'__package__': None,
'allvars_bad': <function allvars_bad at 0xb7d32b1c>,
'd': <Recursion on dict with id=3084093884>,
'fake_temp_var': 'good',
'foo_bad': <function foo_bad at 0xb7d329cc>,
'foo_good': <function foo_good at 0xb7d32f0c>,
'isreadable': <function isreadable at 0xb7d32c34>,
'isrecursive': <function isrecursive at 0xb7d32c6c>,
'pformat': <function pformat at 0xb7d32bc4>,
'pprint': <function pprint at 0xb7d32b8c>,
'saferepr': <function saferepr at 0xb7d32bfc>,
'x': 5}
Note that in the second output, we have overwritten fake_temp_var
, and x is present; the first output only included the local vars within the scope of allvars_bad
.
So if you want to access the full variable scope, you cannot put locals() inside another function.
I had suspected there was some sort of frame object, I just didn't (know where to) look for it.
This works to your spec, I believe:
def allvars_good(offset=0):
frame = sys._getframe(1+offset)
d = frame.f_globals
d.update(frame.f_locals)
return d
def foo_good2():
a = 1
b = 2
return allvars_good()
-->
{'PrettyPrinter': <class pprint.PrettyPrinter at 0xb7d6474c>,
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': 'temp.py',
'__name__': '__main__',
'__package__': None,
'a': 1,
'allvars_bad': <function allvars_bad at 0xb7d65b54>,
'allvars_good': <function allvars_good at 0xb7d65a04>,
'b': 2,
'foo_bad': <function foo_bad at 0xb7d65f44>,
'foo_good': <function foo_good at 0xb7d65f7c>,
'foo_good2': <function foo_good2 at 0xb7d65fb4>,
'isreadable': <function isreadable at 0xb7d65c6c>,
'isrecursive': <function isrecursive at 0xb7d65ca4>,
'pformat': <function pformat at 0xb7d65bfc>,
'pprint': <function pprint at 0xb7d65bc4>,
'saferepr': <function saferepr at 0xb7d65c34>,
'sys': <module 'sys' (built-in)>}