Get locals from calling namespace in Python

前端 未结 3 1135
野性不改
野性不改 2020-11-30 05:00

I want to retrieve the local variables from Python from a called function. Is there any way to do this? I realize this isn\'t right for most programming, but I am basically

3条回答
  •  情深已故
    2020-11-30 05:04

    If you're writing a debugger, you'll want to make heavy use of the inspect module:

    def show_callers_locals():
        """Print the local variables in the caller's frame."""
        import inspect
        frame = inspect.currentframe()
        try:
            print(frame.f_back.f_locals)
        finally:
            del frame
    

提交回复
热议问题