Suppose there is such a piece of code:
def demo():
a = 1
b = 0
c = a / b
def main():
try:
demo()
except Exception as e:
print(e)
main()
Now we hope that the values of a and b in the demo can be obtained without making any modifications to the code in the demo function when an exception occurs.
Referred to this problem and solved it.
Nested function gets calling function
Can be achieved using global variables
global
Function plus reference, this is the knowledge of variable scope
Or put a and b in a dictionary variable, so that global is not needed