- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 119字
- 2021-07-02 14:00:32
Within a Python script
The previous two techniques will start the debugger at the beginning of a Python program. But this third technique is best for long-running processes. To start the debugger within a script, use set_trace().
Now, modify your pdb_example.py file as follows:
import pdb
class Student:
def __init__(self, std):
self.count = std
def print_std(self):
for i in range(self.count):
pdb.set_trace()
print(i)
return
if __name__ == '__main__':
Student(5).print_std()
Now, run the program as follows:
student@ubuntu:~$ python3 pdb_example.py
> /home/student/pdb_example.py(10)print_std()
-> print(i)
(Pdb) continue
0
> /home/student/pdb_example.py(9)print_std()
-> pdb.set_trace()
(Pdb)
set_trace() is a Python function, therefore you can call it at any point in your program.
So, these are the three ways by which you can start a debugger.
推薦閱讀
- Python程序設(shè)計(jì)(第3版)
- 算法精粹:經(jīng)典計(jì)算機(jī)科學(xué)問題的Java實(shí)現(xiàn)
- Spring Cloud、Nginx高并發(fā)核心編程
- Java性能權(quán)威指南(第2版)
- 網(wǎng)站構(gòu)建技術(shù)
- Multithreading in C# 5.0 Cookbook
- 軟件測(cè)試實(shí)用教程
- Python 3.7從入門到精通(視頻教學(xué)版)
- Learning Modular Java Programming
- 大話Java:程序設(shè)計(jì)從入門到精通
- FPGA嵌入式項(xiàng)目開發(fā)實(shí)戰(zhàn)
- Node.js區(qū)塊鏈開發(fā)
- Python應(yīng)用開發(fā)技術(shù)
- HTML5程序開發(fā)范例寶典
- 算法技術(shù)手冊(cè)