Junior — Middle
Are you familiar with the decorator pattern concept and have you used it in your projects?
sobes.tech AI
Answer from AI
The decorator pattern is a structural design pattern that allows adding new responsibilities to objects dynamically by wrapping them in other decorator objects.
In Python, decorators are often implemented as functions or classes that take a function or object and return an extended version with additional behavior.
Example of a simple function decorator:
def decorator(func):
def wrapper(*args, **kwargs):
print("Before calling the function")
result = func(*args, **kwargs)
print("After calling the function")
return result
return wrapper
@decorator
def say_hello():
print("Hello!")
say_hello()
In projects, I have used decorators for logging, access control, caching function results, and other tasks where it is necessary to add functionality without modifying the original code of the function or class.