Does python debugger (pdb) keep fiddling with global variables within the function scope?

Asked 2 years ago, Updated 2 years ago, 18 views

I was playing with the following code to check the behavior of pdb in vscode.
Bring the breakpoint to the print ("start") on the second line from the bottom and start
I will proceed with step into .
After a=100, when the cursor comes to print(a),
In GUI operation, if a=1000, the number will be updated, but it will be returned to 100 immediately. (Not updated?)
You cannot update the global variable from DEBUG CONSOLE in the first place.
In pdb, is there a way to enter or update global variables using DEBUG CONSOLE, etc. within a function?

defmain():
    global a
    a = 100
    print(a)
    func1()


def func1():
    b = 200
    print(b)
    func2()

def func2():    
    c=300
    print(c)
    func3()

def func3():
    d = 400
    print(d)

print ("start")
main()

python

2022-09-30 17:43

1 Answers

When you enter print in main(), it is not reflected as globala;a=1000 from the debug console, or simply by modifying it to a:100 <1000 in the VARIABLES frame, it is not reflected in the output of the print function.

In the CALL STACK frame, you must change it to the stack frame that you want to be interested in (what level you are at if you are in a hierarchy of functions, or the global variable is the best), and then enter the above.

Enter a description of the image here


2022-09-30 17:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.