Home
Locate
CS 101
CS 496
Login
Tutors
Marks
About
Send
Close
Add comments:
(status displays here)
Got it!
This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.
nbsp; Note: This appears on each machine/browser from which this site is accessed.
Python: Stack traces
by RS
admin@ycp.powersoftwo.org
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. Python: Stack traces
Stack traces can be useful for both debugging when errors are raised or certain conditions happen. Here is the Python code.
import traceback def add5(x1, y1): z1 = x1+y1 trace1 = "\n".join(traceback.format_stack()) print("Trace:") print("{0:s}".format(trace1)) return z1 def add4(x1, y1): z1 = add5(x1,y1) return z1 def add3(x1, y1): z1 = add4(x1,y1) return z1 def add2(x1, y1): z1 = add3(x1,y1) return z1 def add1(x1, y1): z1 = add2(x1,y1) return z1 z1 = add1(2,3) print("z={0:d}".format(z1))
Here is the output of the Python code.
Trace: File "D:\W\PROGRAM1.PY\time31-01.py", line 35, in <module> z1 = add1(2,3) File "D:\W\PROGRAM1.PY\time31-01.py", line 31, in add1 z1 = add2(x1,y1) File "D:\W\PROGRAM1.PY\time31-01.py", line 25, in add2 z1 = add3(x1,y1) File "D:\W\PROGRAM1.PY\time31-01.py", line 19, in add3 z1 = add4(x1,y1) File "D:\W\PROGRAM1.PY\time31-01.py", line 13, in add4 z1 = add5(x1,y1) File "D:\W\PROGRAM1.PY\time31-01.py", line 5, in add5 trace1 = "\n".join(traceback.format_stack()) z=5
2. End of page
by RS
admin@ycp.powersoftwo.org