当前位置:网站首页>Pycharm common functions - breakpoint debugging

Pycharm common functions - breakpoint debugging

2022-06-27 20:36:00 I am fat tiger

PyCharm - Breakpoint debugging

I used to write Python The program , Because there is no breakpoint debugging , So debugging always uses print( Variable ) * N Time , debug Very inefficient , often 1 I have to check this question for a long time ...

I learned it later Pycharm Breakpoint debugging , Discovery is really powerful , debug The efficiency of ! So write an article to share this skill , I hope you won't worry about debugging !

Send the debugging code first

import requests


fruit = 'watermelon'

def debug_a():
    global fruit
    a = 0
    for i in range(100):
        a += 1
    fruit = 'Strawberry'


def debug_b():
    c = 20
    d = 30
    e = 40

    debug_a()
    debug_c()
    print(fruit)

    url = "http://httpbin.org/user-agent"
    res = requests.get(url)
    print(res.json())

    b = 0
    for j in range(100):
        b += 1


def debug_c():
    global fruit
    fruit = 'lemon'


if __name__ == '__main__':
    debug_b()

01

Open the debug

At present, there are 4 A breakpoint ,. The location of the break point , See the picture

After setting the breakpoint , Click the button as shown in the figure , Enter debugging mode

02

Toolbar area

Left toolbar

Top Toolbar

03

Basic debugging

First of all Variable area introduction

You can click the buttons in the figure first ( Shortcut key F8), Observe variables Data changes of variables in the region

Click on the straight F8, You can find fruit Variables will be changed after running debug_c Changes after the method , c, d, e The variables do not change .

and , The data of the variable will be at the end of each line , Use gray text to display in the back , It's very convenient to check . This completes the preliminary debugging , Is it simple !

hot tip : Lines marked with blue bars , For example debug_a, Means that this line of code has not been run yet , And about to run !

04

Advanced debugging

Now re debug , But don't F8 了 , Now press A Button debugging . In operation , You can find that you will enter debug_a Methods the internal , Now you can press C The button jumps out . Empathy , In the future, we will also enter debug_c and requests Internal code in .

Debug again , At this time, keep pressing B Button , It can be found that this time it will only enter debug_a and debug_c Methods the internal , Instead of going into requests Inside of . This is it. A Button and B Differences between buttons : During commissioning , Whether to only enter the method defined by yourself !

Now let's talk about watch The use of the zone , The first is to open watch Area button , Pictured

watch Area interface , As shown in the figure

watch The role of the zone : When variables When the zone has many variables , At this point, you can select the variable of interest to watch District

That's all Pycharm Common debugging skills , You've learned ?

原网站

版权声明
本文为[I am fat tiger]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206271756572460.html