How to get value of arguments passed to functions on the stack?

后端 未结 2 1047
暗喜
暗喜 2021-01-11 09:37

Using:

traceback.print_stack()

I can get:

  File \"x.py\", line 20, in 
    y(x)
  File \"x.py\", line 11, in         


        
2条回答
  •  失恋的感觉
    2021-01-11 10:26

    You can use the inspect module for this:

    >>> import inspect
    ... def fn(x):
    ...     try:
    ...         print(1/0)
    ...     except ZeroDivisionError as e:
    ...         frames = inspect.trace()
    ...         argvalues = inspect.getargvalues(frames[0][0])
    ...         print("Argvalues: ", inspect.formatargvalues(*argvalues))
    >>> fn(12)
    Argvalues:  (x=12)
    

提交回复
热议问题