- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 pdb(或 ipdb)中,我们可以使用 !
or p
commands 执行语句和计算表达式:
p expression
Evaluate the expression in the current context and print its value.[!]statement
Execute the (one-line) statement in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a global command on the same line
因此,例如,我可以在 ipdb 中调试时键入 p reddit.get_subreddits()
,代码将在当前上下文中执行,我将看到返回值。
有没有办法调试这种“手动输入”表达式的执行?
基本上我想做的是 s reddit.get_subreddits()
,但这只是执行 step
命令并忽略表达式。
编辑:一个简单的例子
采用这个简单的函数:
import random
def get_value_for_weekday(weekday_index=None):
values = [10, 20, 20, 10, 30, 30, 30]
if not weekday_index:
# If no weekday provided, return the average of all weekdays
return sum(values) / 7
return averages[weekday_index]
if __name__ == '__main__':
while True:
import ipdb; ipdb.set_trace() # enter ipbd for debug
get_value_for_weekday(random.randint(0, 7))
因为 if not weekday_index
而被窃听(它应该检查 weekday_index is not None
。)
假设我注意到我得到 10
的次数是我预期的一半。所以我添加了一个 import ipdb; ipdb.set_trace()
在调用函数之前尝试调试代码。
所以我在 ipdb 控制台中,我突然想到问题可能出在我将 0 作为 weekday_index 传递时。我可以直接在 ipdb 中测试我的假设:
ipdb> p get_value_for_weekday(0)
22
好的,所以当 weekday_index=0
时我意识到有问题。
我现在想做的是逐步调试对 get_value_for_weekday(0)
的调用,这样我就可以看到我错误地进入了 if block 。
显然我可以退出 ipdb,停止脚本,将代码更改为始终传递 0,重新启动脚本,当我进入 ipdb 时,使用 ipdb step
(s
) 命令。
但是,如果我可以像执行 p get_value_for_weekday(0)
一样执行 s get_value_for_weekday(0)
不是更容易吗?
有没有办法做这样的事情?
最佳答案
我认为您正在寻找 (d)ebug
命令,出于某种原因,Debugger Commands 中未指定该命令.仅供将来引用,pdb
指定了一组很好的命令(您可以通过在交互式提示中键入 help
来查看)。在 debug
命令上:
(Pdb) help debug
debug code
Enter a recursive debugger that steps through the code
argument (which is an arbitrary expression or statement to be
executed in the current environment).
这似乎可以满足您的需求。从终端使用您的示例脚本:
python -m pdb pdbscript.py
在发出两个 n
命令以便解析函数之后(我相信这就是 pdb
的工作方式)。您可以发出 debug get_value_for_weekday(0)
命令以递归步进函数:
(Pdb) debug get_value_for_weekday(0)
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
((Pdb)) s
--Call--
> /home/jim/Desktop/pdbscript.py(3)get_value_for_weekday()
-> def get_value_for_weekday(weekday_index=None):
((Pdb)) n
> /home/jim/Desktop/pdbscript.py(4)get_value_for_weekday()
-> values = [10, 20, 20, 10, 30, 30, 30]
((Pdb)) n
> /home/jim/Desktop/pdbscript.py(5)get_value_for_weekday()
-> if not weekday_index:
((Pdb)) p weekday_index
0
((Pdb)) n
> /home/jim/Desktop/pdbscript.py(7)get_value_for_weekday()
-> return sum(values) / 7
请注意,我对这种形式的元调试非常粗略,但这似乎正是您所追求的。
关于python - 如何在 pdb 中调试手动键入的表达式和语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769636/
考虑代码: def foo() -> ??: return bar() 我怎么能说我希望 foo 的返回类型与 bar() 的返回类型相同? 在 C++ 中,我可以使用 decltype,类
我运行以下 mysql 查询并看到第一个查询的类型为 ALL。 mysql> EXPLAIN SELECT one.language_id as filter_id,
http://muaddibspace.blogspot.com/2008/01/type-inference-for-simply-typed-lambda.html是 Prolog 中简单类型 l
鉴于类型 type EnumerableComponentFactory = (config: { Container: React.ComponentType; Item: React.Co
我在我的自动 rmarkdown pdf 文档中使用 stargazer 包来制作漂亮的表格。默认情况下,Stargazer 将其表格放置在页面的中心。如何让观星者生成将表格与左侧对齐的 latex
class A: pass class B(A): pass ListOfA = List[A] list_of_a : ListOfA = [A(), A()] for e in [
我目前正在研究学习 WPF 和 Silverlight。到目前为止,我一直在用 Delphi 和 C# (Winforms) 进行开发。在看一些关于 WPF 的书籍和在线观看一些介绍性视频时,我的印象
在输入如下内容后: ) 按钮。但是那个按钮比较不方便。 (请注意,对于 Orientation="Horizontal" 等下拉选项不存在此问题,从下拉菜单中选择 Horizontal 后,插
我在输入以下内容时遇到问题。 问题在于 TeamIcon . 我的对象定义如下。 import TeamIcon from './components/icons/TeamIcon'; export
Demo 我这里有一个验证类是否存在于 div 中的演示。当没有类时,另一个类将从按钮中删除。基本上,当没有禁用按钮时,添加按钮上的禁用将被删除。 如果用户做的最后一件事是更改,这可以正常工作,但如果
这个问题在这里已经有了答案: How can I access object properties containing special characters? (2 个答案) 关闭 9 年前。 我
尝试创建一个 Web 应用程序(使用 mySQL 和 Python),其中包含马萨诸塞州的徒步旅行路线列表。我只想在一页上显示我的数据库中所有路径的名称,但不知道为什么什么都不显示: ########
您好,我想在使用 jQuery 的文本输入字段中键入时用点替换逗号。我现在有这段代码; $(document).on('change', '.unitprice', function() { $(
我为静态主页编写了以下代码。在这里我想使用类型化的库,它包含在部分“head.html”中。但是由于键入我的内容不断跳跃。这是因为在某个时刻字幕容器是空的。我试过添加默认占位符,但这似乎无法解决问题。
我想知道有没有类似于scanf的函数允许在c中自定义输入。我想要做的是当用户输入想要的日期时,每隔几个字符添加一个点。当用户想要确认他的输入时,他应该按下回车键,并且该变量被保存在某处。 示例:用户输
Java 中有没有办法让 ListModel 只接受特定类型?我要找的是类似 DefaultListModel 的东西或 TypedListModel ,因为 DefaultListModel 只实现
考虑如下数组类型: let example1: MyArray = ['John'], example2: MyArray = [4, 5, 1, 5, 'Eric'], exampl
我想弄清楚我是否正确输入了我的 React 高阶组件。在大多数情况下,这是正常工作的,但是在将 React 引用应用于 HOC 实例时,我遇到了输入问题。下面是一个简化的重现: import * as
这是 Python 3.7 我有一个这样的数据类: @dataclass class Action: action: str 但 Action 实际上仅限于值“bla”和“foo”。有没有一种明
我想弄清楚我是否正确输入了我的 React 高阶组件。在大多数情况下,这是正常工作的,但是在将 React 引用应用于 HOC 实例时,我遇到了输入问题。下面是一个简化的重现: import * as
我是一名优秀的程序员,十分优秀!