+ 8
Can some explain Decorators more deeply?
Please include examples.
2 ответов
+ 2
When you have a function you can modify by decorators so you need the function in another function to modify before the execution or and after it . example:
def decor_example(func_to_decore,a, b):
def decor_aux():
#you can add code before
print("before before\n")
#execute the original function
func_to_decore()
#you can add code after the execution
c=a+b
print(c)
return decor_aux
def aa():
print("la la la\n")
decorated = decor_example(aa,100,200)
decorated()
if you just call aa() the result is "la la la" but is you use a decore....
decorated()
you are sending the function aa and two parameters 100 and 200 and the result is
"before before
la la la
300"
+ 2
Here is an example of what a decorator may be useful for:
https://code.sololearn.com/cD09ijlIS6Ev/?ref=app