官术网_书友最值得收藏!

Debugging basic program crashes

In this section, we are going to see the trace module. The trace module helps in tracing the program execution. So, whenever your Python program crashes, we can understand where it crashes. We can use trace module by importing it into your script as well as from the command line.

Now, we will create a script named trace_example.py and write the following content in the script:

class Student:
def __init__(self, std):
self.count = std

def go(self):
for i in range(self.count):
print(i)
return
if __name__ == '__main__':
Student(5).go()

The output will be as follows:

student@ubuntu:~$ python3 -m trace --trace trace_example.py
--- modulename: trace_example, funcname: <module>
trace_example.py(1): class Student:
--- modulename: trace_example, funcname: Student
trace_example.py(1): class Student:
trace_example.py(2): def __init__(self, std):
trace_example.py(5): def go(self):
trace_example.py(10): if __name__ == '__main__':
trace_example.py(11): Student(5).go()
--- modulename: trace_example, funcname: init
trace_example.py(3): self.count = std
--- modulename: trace_example, funcname: go
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
0
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
1
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
2
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
3
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
4

So, by using trace --trace at the command line, the developer can trace the program line-by-line. So, whenever the  program crashes, the developer will know the instance where it crashes.

主站蜘蛛池模板: 察雅县| 湟源县| 太湖县| 保德县| 洛浦县| 蒙阴县| 莱阳市| 定西市| 年辖:市辖区| 锦州市| 呼图壁县| 上思县| 广德县| 仁怀市| 秦皇岛市| 巨野县| 乌兰县| 平阴县| 岚皋县| 泰来县| 广宗县| 嵊泗县| 卓资县| 鸡西市| 安图县| 金坛市| 平顶山市| 大理市| 青神县| 长沙市| 石狮市| 五河县| 闽侯县| 富蕴县| 工布江达县| 天水市| 桑日县| 伊通| 泽普县| 浠水县| 伊春市|