Suppose one of them had the following:
def outer(foo, bar, baz): frobozz = foo + bar + baz def inner(x, y, z): return dict(globals().items() + locals().items()) return inner(7, 8, 9)
The value returned by inner is the dictionary obtained by merging the dictionaries returned by globals() and locals() , as shown. In general case 1, this return value will not contain entries for foo , bar , baz and frobozz , although these variables are visible inside inner , and therefore, possibly belong to the inner namespace.
One way to make it easier to capture the inner namespace is to use the following kluge:
def outer(foo, bar, baz): frobozz = foo + bar + baz def inner(x, y, z, _kluge=locals().items()): return dict(globals().items() + _kluge + locals().items()) return inner(7, 8, 9)
Is there a better way to capture the inner namespace than this kluge type?
1 If variables with these names are not present in the current global namespace.
source share