Dump function variables to workspace in python/ipython

前端 未结 5 979
闹比i
闹比i 2021-01-14 10:06

When developing and debugging with python/ipython repl, at some point I\'d like to dump all the local variables in a function to the workspace to see what\'s going on. Suppo

相关标签:
5条回答
  • 2021-01-14 10:16

    This is a hugly hack, but I use it very often in my jupyter notebooks for dumping some local state to the global workspace for further inspection. Simply add these two lines to the end of your function and then you'll have access to all the local variables directly from the notebook:

    import inspect
    inspect.getmodule(next(frm[0] for frm in reversed(inspect.stack())
                           if frm[0].f_locals.get('__name__', None) == '__main__')).__dict__.update(locals())
    

    What it does is traverse the stack in reverse order (using the inspect module) to find the top-most module named '__main__'. That's the module representing the notebook (i.e. the current kernel). It then updates the module's global variables definition using __dict__ with the function's locals (using __locals__)

    Here's a demo in a notebook: https://colab.research.google.com/drive/1lQgtmqigCUmzVhkX7H7azDWvT3CfxBtt

    #%%
    def some_calculation_of_a_and_b(a, b):
        return 'some_calculated_value'
    
    def func():
        a = "blablabla"
        b = 1234
        c = some_calculation_of_a_and_b(a,b)
    
        #dump_all_local_variables_to_workspace():
        import inspect
        inspect.getmodule(next(frm[0] for frm in reversed(inspect.stack())
                          if frm[0].f_locals.get('__name__', None) == '__main__')).__dict__.update(locals())
    
    #%%
    func()
    print(a)
    print(b)
    print(c)
    
    # this will print:
    # blablabla
    # 1234
    # some_calculated_value
    
    0 讨论(0)
  • 2021-01-14 10:25

    To expand on my comment above, here's a quick intro to pdb:

    import pdb
    def func():
        a = "blablabla"
        b = 1234
        c = some_calculation_of_a_and_b(a,b)
        pdb.set_trace()
    

    To run:

    python program.py

    The interpreter will then stop at the pdb.set_trace() line allowing you to observe the values of a,b and c.

    While stopped, you can print the values of the local variables by typing:

    p a
    p b
    

    etc. A full list of commands in pdb can be obtained by typing ? at the pdb prompt. Here's a link to the library documentation.

    0 讨论(0)
  • 2021-01-14 10:26

    you could use whos in ipython. vars() in python environment.

    0 讨论(0)
  • 2021-01-14 10:34

    You have a function for this:

    print locals()
    

    and globals() for global variables

    0 讨论(0)
  • 2021-01-14 10:41

    To achieve what is directly asked for in the question, the function could return locals(), then you can update locals() in the interactive environment:

    def func():
        # define a,b,c
        return locals() # or, more restrictively, return {'a':a, 'b':b, 'c':c}
    
    >>> locals().update(func())
    
    0 讨论(0)
提交回复
热议问题