在任何IDE中是否有调试功能可以让您看到代码的“路径”?

2024-10-03 09:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在调试一段巨大的代码,这是一项巨大的任务。代码包含许多条件(if/else)语句,为了使调试工作更容易,我想看看解释器通过所有if/else的“路径”。你知道吗

例如:

if stuff:
    x = "stuff"
elif otherstuff:
    x = "otherstuff"
else:
    x = "evenmorestuff"
return x

我的实际代码要复杂得多,用return代替x不是一个选择。但我希望你能明白。你知道吗

我想看看当我的代码崩溃时,通过ifs、elifs和elses的“路径”是什么。例如:

|  if stuff:
>      x = "stuff"            (ran this line)
 |     if morestuff:
 |         y = "morestuff"    (skipped this line)
 |     else:
 >         y = "nostuff"      (ran this line)
  |elif otherstuff:
  |    x = "otherstuff"       (skipped this line)
  |else:
  |    x = "evenmorestuff"    (skipped this line)
  >return x                   (ran this line)

在任何IDE中,有什么特性可以做到这一点吗?(我正在使用带有PTVS和Python的visualstudio Community 2015,所以如果有解决方案的话,那就太好了,因为我不必切换ide。)我只是不知道它叫什么,当我搜索类似于这个标题的内容时,google搜索没有结果,所以我决定在这里询问。你知道吗

谢谢:)


Tags: 代码路径returniflinethis条件else
1条回答
网友
1楼 · 发布于 2024-10-03 09:12:18

你要找的动词是“追踪”。你知道吗

Python有一个trace模块可以做到这一点。我在网上找到文章 tracing in generalthe module itself看起来很有用。你知道吗

The trace module helps you understand the way your program runs. You can trace the statements executed, produce coverage reports, and investigate the relationships between functions that call each other.

您可能会发现,将代码重构为更小的函数可以更容易理解和/或跟踪其执行情况。你知道吗

相关问题 更多 >