- 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.
推薦閱讀
- Learning C# by Developing Games with Unity 2020
- Instant Apache Stanbol
- C/C++算法從菜鳥到達人
- MySQL 8 DBA基礎教程
- C語言課程設計
- 利用Python進行數據分析(原書第3版)
- PySpark Cookbook
- Learning Concurrency in Kotlin
- Building Microservices with .NET Core
- 移動互聯網軟件開發實驗指導
- Kivy Cookbook
- Learning VMware vSphere
- Flink核心技術:源碼剖析與特性開發
- 例說FPGA:可直接用于工程項目的第一手經驗
- Java面試一戰到底(基礎卷)