Junior — Middle
Have you participated in implementing or using decorators in your past projects?
sobes.tech AI
Answer from AI
Yes, I have used decorators in Python to add additional functionality to functions without changing their original code. For example, I used decorators for logging function calls or access rights verification.
Here's a simple decorator example that outputs the execution time of a function:
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"Execution time of {func.__name__}: {end - start:.4f} seconds")
return result
return wrapper
@timer
def some_function():
time.sleep(1)
some_function()