How to print all variables values when debugging Python with pdb, without specifying each variable?

后端 未结 2 929
温柔的废话
温柔的废话 2021-01-30 01:49

I\'m debugging my Python scripts using pdb and the manual says I can use p variables command to print the values of the specified variables at a certai

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 02:12

    As given in this list for multiple languages:

    a = 1
    b = 1
    n = 'value'
    #dir() == ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'n']
    for var in dir()[4:]:
        value_of_var = eval(var)
        print(value_of_var)
    

    Output:

    1
    1
    'value'
    

    Labelling each one is as simple as printing var + " equals " + eval(var).

    You state your "ideal output" is exactly the result as typing p a, b, ... , n, ...:

    vars = []
    for var in dir()[4:-1]
        vars.append(var)
    print(tuple(vars))
    

    Output looks like:

    (1, 1, 'value')
    

提交回复
热议问题