How can I get access to the complete namespace, including nonlocal names, when for instance doing code.interact in Python (v. 3.6)?
Often suggested is the following solution or some equivalent:
code.interact(local={**globals(), **locals()})
This however does not provide the nonlocal names (those of an outer function seen from an inner function). In fact, even the builtin function dir() seems to behave funny in this respect:
def f():
x = 1
def g():
print('g:', dir())
# print(x)
g()
print('f:', dir())
The name x is invisible to dir() in the above code, but is visible if the print(x) line is uncommented. It's funny how the semantics of dir() should depend on what happens after it?! (Is this an optimization or a bug or what?) So is there a way to get all nonlocal names without actually having to access them first?
I would prefer not having to write a function of my own (using inspect or whatever).