跳转到主要内容

显示调用栈的简化视图。

项目描述

显示调用栈

显示调用栈的简化视图。

此模块类似于Python内置的tracebackinspect模块,但更容易使用,并且显示更简单的输出。此模块在演示递归函数调用期间的调用栈时很有用。只需添加一行from showcallstack import showCallStack,然后从您希望查看调用栈状态和每个调用帧中局部变量的位置调用showCallStack()

示例用法

此程序...

from showcallstack import showcallstack

def a():
    varA = 42
    b()

def b():
    varB = 86
    c()

def c():
    varC = 99
    showcallstack()

spam = 'SPAM!
a()

...输出如下

The call stack is 3 call(s) deep:
(Here is the "bottom" of the call stack.)
Local variables of call to a():
  varA (type: int) == 42
Local variables of call to b():
  varB (type: int) == 86
Local variables of call to c():
  varC (type: int) == 99
(Here is the "top" of the call stack.)

Global variables:
  spam (type: str) == 'SPAM!'

此递归阶乘程序...

from showcallstack import showcallstack

def factorial(num):
    showcallstack()
    if num == 1:
        return 1
    else:
        return num * factorial(num - 1)

factorial(4)

...输出如下

The call stack is 1 call(s) deep:
(Here is the "bottom" of the call stack.)
Local variables of call to factorial():
  num (type: int) == 4
(Here is the "top" of the call stack.)

Global variables:
  No global variables.

The call stack is 2 call(s) deep:
(Here is the "bottom" of the call stack.)
Local variables of call to factorial():
  num (type: int) == 4
Local variables of call to factorial():
  num (type: int) == 3
(Here is the "top" of the call stack.)

Global variables:
  No global variables.

The call stack is 3 call(s) deep:
(Here is the "bottom" of the call stack.)
Local variables of call to factorial():
  num (type: int) == 4
Local variables of call to factorial():
  num (type: int) == 3
Local variables of call to factorial():
  num (type: int) == 2
(Here is the "top" of the call stack.)

Global variables:
  No global variables.

The call stack is 4 call(s) deep:
(Here is the "bottom" of the call stack.)
Local variables of call to factorial():
  num (type: int) == 4
Local variables of call to factorial():
  num (type: int) == 3
Local variables of call to factorial():
  num (type: int) == 2
Local variables of call to factorial():
  num (type: int) == 1
(Here is the "top" of the call stack.)

Global variables:
  No global variables.

您还可以调用showcallstack.getcallstack()函数以获取作为字符串列表的输出。

支持

如果您发现此项目有帮助并希望支持其开发,请考虑在Patreon上向其创作者捐款

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

ShowCallStack-0.3.0.tar.gz (4.2 kB 查看哈希)

支持