Junior — Middle
Have you had experience using decorators in your projects?
sobes.tech AI
Answer from AI
Yes, in projects I have used decorators to extend the functionality of functions and methods without changing their original code. For example, for logging function calls, measuring execution time, or access rights verification.
Here's a simple decorator example for logging:
def log_calls(func):
def wrapper(*args, **kwargs):
print(f"Calling function {func.__name__} with arguments {args} {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)
Decorators help write cleaner and more maintainable code, especially in test automation, where it is often necessary to add repetitive actions around test functions.