Junior — Middle
What information does a decorator function in Python return?
sobes.tech AI
Answer from AI
A decorator function in Python is a function that takes another function and returns a new function (or an object callable as a function). That is, the result of the decorator is a wrapper around the original function, which can modify or extend its behavior.
Example of a simple 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(name):
print(f"Hello, {name}!")
say_hello("World")
Here, decorator returns the wrapper function, which replaces the original say_hello. Thus, the decorator function returns a new function with modified behavior.