Skip to content

Instantly share code, notes, and snippets.

@EteimZ
Created December 30, 2023 11:18
Show Gist options
  • Save EteimZ/3db4c8dbe670c9f00c669bc83716bdca to your computer and use it in GitHub Desktop.
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
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