Created
December 30, 2023 11:18
-
-
Save EteimZ/3db4c8dbe670c9f00c669bc83716bdca to your computer and use it in GitHub Desktop.
A decorator that keep track of a function when it has been called. This is good for viewing recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
def log_function_call(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}") | |
result = func(*args, **kwargs) | |
print(f"{func.__name__} returned: {result}") | |
return result | |
return wrapper | |
@log_function_call | |
def recursive_factorial(n): | |
if n == 0 or n == 1: | |
return 1 | |
else: | |
return n * recursive_factorial(n - 1) | |
# Test the recursive function | |
result = recursive_factorial(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment