I have the following scenario, where I want to get frame info for the scope above my scope.
def outer():
# This is really slow.
frames = inspect.stack()
def inner():
# I would like to "pay for it" here instead.
print(frames)
return inner
The call inspect.stack() is very slow, so I would like to do it only when I really need to, meaning when someone calls inner. But if I move it inside the inner function, I would get different frames than what I need.
Alternatively if I could get all of frame.filename and frame.lineno, (for each frame for the outer scope), fast in some other way - that is also good.