A decorator is a function that decorates a function that is a function for a decorator and replaces it with a decorated function.
For example, prepare the following function for the decorator.
def my_decorator(func):
def decorated_my_func(*args, **kwargs):
print("Decorated")
func(*args, **kwargs)
return decorated_my_func
Then decorate and declare an ordinary function with the above function.
@my_decorator
def my_func(str):
print(str)
And when you run
>>> my_func("Ah ah ah")
Decorated
Ah ah ah
It becomes. That is, by adding @my_decorator
when declaringmy_func ()
>>> my_func = my_decorator(my_func)
... my_func("Ah ah ah")
Decorated
Ah ah ah
Decorators do the same thing as.
Recommended Posts