recursion - How to print the recursive stack in Python -
how print or show recursive stack in python when i'm running recursive function?
it's not clear want far question, can print stack of function callers in recursive manner following, using python inspect module.
import inspect, sys max_recursion_depth = 10 def rec_func(recursion_index): if recursion_index == 0: return rec_func(recursion_index-1) current_frame = inspect.currentframe() calframe = inspect.getouterframes(current_frame, 2) frame_object = calframe[0][0] print("recursion-%d: %s" % (max_recursion_depth - recursion_index, frame_object.f_locals)) print("passed parameters: %s" % (sys._getframe(1).f_locals) ) rec_func(max_recursion_depth)
you can use sys.get_frame()
access current frame , using f_locals
property, can access parameters passed current frame, in recursive manner, can observe parameter decreasing.
mostly information want stack accessible frame objects can i've brought above.
Comments
Post a Comment