Senior
Write a decorator that prints 'hello' before a function and 'bye' after, with deferred start.
sobes.tech AI
Answer from AI
A deferred decorator is a function that returns a wrapper function but does not call the decorated function immediately. Instead, it returns a function that can be called later.
Example in Python:
from functools import wraps
def delayed_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
print("hello")
result = func(*args, **kwargs)
print("bye")
return result
def delayed():
return wrapper()
return delayed
@delayed_decorator
def my_function():
print("Executing function")
# Function is not executed immediately
f = my_function
# Deferred execution
f()
Here, after decoration, my_function returns the delayed function, which can be called later. When called, it prints "hello", then executes the function, then "bye".