Creating Breakpoints in pdb set_trace()

Python bilan kodni tuzatishni o'rganish.

SU

set_trace() bilan Breakpoint Yaratish

Python debugger yordamida kodingizga to‘g‘ridan-to‘g‘ri breakpoint qo‘shishingiz mumkin. Buning uchun pdb modulini import qilib, quyidagicha breakpoint yaratiladi:

# debug_code_with_settrace.py
 
def log(number):
    print(f'Processing {number}')
    print(f'Adding 2 to number: {number + 2}')
 
def looper(number):
    for i in range(number):
        import pdb; pdb.set_trace()
        log(i)
 
if __name__ == '__main__':
    looper(5)

Endi bu kodni terminalda ishga tushirsangiz, kod set_trace() chaqirig‘iga yetganda avtomatik ravishda pdb debuggeriga o‘tadi:

$ python3.8 debug_code_with_settrace.py
> /python101code/chapter26_debugging/debug_code_with_settrace.py(12)looper()
-> log(i)
(Pdb)

set_trace() ni Ishlatishning Kamchiliklari

  • Ushbu usulda siz qo‘shimcha kod yozishingiz kerak bo‘ladi, bu esa keyinroq olib tashlanishi talab etiladi.
  • Kodda xatolikka yo‘l qo‘yish ehtimoli mavjud. Masalan, import pdb va pdb.set_trace() o‘rtasida ; (nuqta-vergul) qo‘yishni unutish xatoga olib keladi.

breakpoint() ni Ishlatish

Python ishlab chiquvchilari bu jarayonni soddalashtirish uchun breakpoint() funksiyasini qo‘shdilar. Bu funksiya import pdb; pdb.set_trace() bilan teng kuchga ega.
Keyingi bo‘limda breakpoint() funksiyasidan foydalanishni o‘rganamiz!

Last updated on

On this page

Xato haqida xabar berish