Junior — Middle
Have you developed your own decorators for functions or methods?
sobes.tech AI
Answer from AI
Yes, I have developed custom decorators for functions and methods in Python. Decorators allow you to wrap a function or method with additional functionality without changing their original code. For example, you can create a decorator for logging function calls:
import functools
def log_calls(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print(f"Calling function {func.__name__} with arguments {args} and {kwargs}")
result = func(*args, **kwargs)
print(f"Function {func.__name__} returned {result}")
return result
return wrapper
@log_calls
def add(a, b):
return a + b
add(2, 3)
Such a decorator is useful for debugging and monitoring the operation of functions.