+ 8

Can some explain Decorators more deeply?

Please include examples.

25th Aug 2016, 5:51 AM
Pankaj Sharma
Pankaj Sharma - avatar
2 odpowiedzi
+ 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"
29th Aug 2016, 1:30 AM
Adrian Manrique
Adrian Manrique - avatar
+ 2
Here is an example of what a decorator may be useful for: https://code.sololearn.com/cD09ijlIS6Ev/?ref=app
25th Mar 2018, 4:32 PM
Denys Yeromenko
Denys Yeromenko - avatar