我有以下问题:
我有如下代码
def my_function():
import pdb; pdb.set_trace()
....
在运行时,我的函数被调用了很多次。我有兴趣在第一次调用这个函数时检查代码的执行情况。在
如果我完成了检查,我希望点击c
,并恢复程序的正常执行,而不会在下次调用此函数时在这个断点处停止。在
有办法吗?或者我必须做一些完全不同的事情,比如让set_trace()
调用某个只调用一次的地方,然后在断点被命中后,使用tbreak my_function
这样的命令在那里设置一个一次性断点?在
Tags:
当我遇到这个问题时,我
import pdb; pdb.set_trace()
在文件的开头(导入时间)或在__main__
(运行时)中,然后使用调试器list
(或仅使用l
)命令来查找行号,break
(或只是b
)来设置断点。在您也可以从一开始就以PDB模式启动程序并到达相同的点。在
例如,我将在第5行设置一个断点
^{pr2}$现在,继续
当到达断点时,您将输入Pdb。在
因为您只想看到第一次运行,所以只需删除断点
您可以尝试在第一次执行函数时设置它的属性。比如:
属性
first_time
将在函数调用之间保持,并且在第一次调用函数时,它将被创建。下一次调用函数时,它已经存在,if语句中的代码将不会执行。这个解决方案依赖于你的函数不是类内部的方法,因为类方法不能有属性,因为它们已经是类的属性。在请注意,我不确定您的实际代码中是否有导入,但最佳编码实践表明,您应该只将导入放在代码的开头,而不是像您所拥有的那样将导入放在函数内部。在
相关问题 更多 >
编程相关推荐