python - PDB - Set or change Variable in interactive mode
问题描述
Currently, when I modify variables inside the interactive interpreter in pdb, it doesn't carry over outside the interactive session. Is there a way to do this?(I'm already aware of exec, !). However, I want to perform some multi-line operations.
(Pdb) c
(Pdb) pp locals()['a']
*** KeyError: 'a'
(Pdb) !a=2
(Pdb) pp locals()['a']
2
(Pdb) !del a
(Pdb) pp locals()['a']
*** KeyError: 'a'
(Pdb) interact
*interactive*
>>> a=2
>>>
now exiting InteractiveConsole...
(Pdb) pp locals()['a']
*** KeyError: 'a'
解决方案
尝试下面的代码在调试时执行多行代码pdb/ipdb
!import code; code.interact(local=vars())
这将进入interactive console
仅用于检查您的代码,并且那里的pdb
任何更改将仅适用于该执行并且不会更改您的代码
推荐阅读
- c++ - 如何遍历 QStack 对象的元素?
- python - 如何从先前的解决方案重新初始化 Python Gekko 中的优化
- command - 我想为每个加入服务器的成员创建一个单独的文件,然后能够随机选择它们作为“赢家”命令排序的事情
- r - 如何改变与 R 中的字符串匹配的所有列?
- c# - 如何在 C# 的列表框中填充多行字符串
- vue.js - 将方法动态传递给子组件
- unit-testing - 如何使用 Common Lisp 测试框架“Prove”?
- python - 将 Byte 类型转换为 float
- c++ - 带有 phong 着色和光线追踪的工件
- jquery - 在窗口调整大小时重置由函数设置的高度