【Python 笔记】Ipython中pdb调试器的使用
2016-02-29 19:42
651 查看
在Ipyhton中,调试器增强了pdb,在不使用%pdb的情况下可以有如下两种调试手段:
1、使用 set_trace 这个特别的函数(以 pdb.set_trace 命名),这差不多可以算作是一种“庶民的断点“。
2、使用自定义的断点函数:
def set_trace():
from IPython.core.debugger import Pdb
Pdb(color_scheme = 'Linux').set_trace(sys.getframe().f_back)
def debug(f,*args,**kwargs):
from IPython.core.debugger import Pdb
pdb = Pdb(color_scheme = 'Linux')
return pdb.runcalll(f,*args,**kwargs)
第一个函数set_trace非常简单,可以将其放在代码中任何希望停下来看一看的地方(比如发生异常的地方):
第二个函数debug则使你能够在任意函数上使用调试器:
def f(x,y,z=1):
tmp = x+y
return tmp/z
def debug(f,*args,**kwargs):
from IPython.core.debugger import Pdb
pdb = Pdb(color_scheme = 'Linux')
return pdb.runcall(f,*args,**kwargs)
debug(f,1,2,z=3)
将 f 作为第一个参数传给 debug,后面的参数依次为 f 的关键字参数。
<ipython-input-6-b40372ecb298>(2)f()
1 def f(x,y,z=1):
----> 2 tmp = x+y
3 return tmp/z
ipdb>
如此这般,便可以直接进入到 ipdb 中去了
摘抄自《利用Python进行数据分析》
1、使用 set_trace 这个特别的函数(以 pdb.set_trace 命名),这差不多可以算作是一种“庶民的断点“。
2、使用自定义的断点函数:
def set_trace():
from IPython.core.debugger import Pdb
Pdb(color_scheme = 'Linux').set_trace(sys.getframe().f_back)
def debug(f,*args,**kwargs):
from IPython.core.debugger import Pdb
pdb = Pdb(color_scheme = 'Linux')
return pdb.runcalll(f,*args,**kwargs)
第一个函数set_trace非常简单,可以将其放在代码中任何希望停下来看一看的地方(比如发生异常的地方):
第二个函数debug则使你能够在任意函数上使用调试器:
def f(x,y,z=1):
tmp = x+y
return tmp/z
def debug(f,*args,**kwargs):
from IPython.core.debugger import Pdb
pdb = Pdb(color_scheme = 'Linux')
return pdb.runcall(f,*args,**kwargs)
debug(f,1,2,z=3)
将 f 作为第一个参数传给 debug,后面的参数依次为 f 的关键字参数。
<ipython-input-6-b40372ecb298>(2)f()
1 def f(x,y,z=1):
----> 2 tmp = x+y
3 return tmp/z
ipdb>
如此这般,便可以直接进入到 ipdb 中去了
摘抄自《利用Python进行数据分析》
相关文章推荐
- python iris 数据集
- Python并发编程
- python ipython notebook教程
- python入门
- python中的lambda表达
- python学习笔记七 初识socket(进阶篇)
- Python——正则表达式(2)
- python成长之路第三篇(2)_正则表达式
- python Json模块
- Python 优化集锦
- Python爬虫扒取2345天气预报网上的邯郸天气数据
- 【python 笔记】 python pdb 调试技巧
- Python基础篇【第7篇】: 面向对象(1)
- Python基础篇【第6篇】: Python模块subprocess
- Python基础篇【第6篇】: Python装饰器
- 17个新手常见Python运行时错误
- [python实现设计模式]-1. 单例模式
- Celery任务调度示例
- Python——正则表达式(1)
- python爬虫提取冰与火之歌五季的种子