forked from LinkedInLearning/python-recursion-2875238
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace_recursion.py
More file actions
26 lines (20 loc) · 749 Bytes
/
Copy pathtrace_recursion.py
File metadata and controls
26 lines (20 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from functools import wraps
def trace(func):
# Store function name, for later use
func_name = func.__name__
separator = '| ' # Used in trace display
# Set the current recursion depth
trace.recursion_depth = 0
@wraps(func)
def traced_func(*args, **kwargs):
# Display function call details
print(f'{separator * trace.recursion_depth}|-- {func_name}({", ".join(map(str, args))})')
# Begin recursing
trace.recursion_depth += 1
result = func(*args, **kwargs)
# Exit current level
trace.recursion_depth -= 1
# Display return value
print(f'{separator * (trace.recursion_depth + 1)}|-- return {result}')
return result
return traced_func