[ ]
宙飒zhousa.om 1. 代码调试
代码调试主要是debug,也就是确保程序不出错误.基本可以分为如下几个方面:
- 单步调试,一步一步的查看代码的运行状态
- 调用追踪,查看函数报错在堆栈中的状况
- 段错误追踪,一般用于C写扩展的模块追踪内存泄漏等错误
1.1. 单步调试模块
pdb是python自带的调试模块,它可以在交互环境中使用,也可以在terminal中作为python的一个模式使用
要调试的脚本:
#!/usr/bin/env python3 class Counter(object): """一个计数器 用法: >>> counter1 = Counter() >>> counter1() 1 >>> counter1() 2 >>> counter2 = Counter(lambda : 2,-3) >>> counter2() -1 >>> counter2() 1 """ def __str__(self): return "state:"+str(self.value) def __repr__(self): return self.__str__ def __call__(self): def count(): self.value += self.func() return self.value return count() def __init__(self,func=lambda : 1,start=0): self.value = start self.func = func test = Counter() test() test() print(test) if __name__=="__main__": counter1 = Counter() counter2 = Counter() for i in range(10): counter1() for i in range(8): counter2() if counter1.value == counter2.value: print("not success") else: print("don't known") import doctest doctest.testmod(verbose=True)
命令行调试
python -m pdb counter.py
在交互shell中调试
import pdb import counter pdb.run('counter.test()')
常用的调试命令可以在调试模式下用help命令来查看
还没有评论,来说两句吧...