Please, explain me how this decorator works! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Please, explain me how this decorator works!

from playground https://code.sololearn.com/cs1eRY1k7yRa/?ref=app

31st Oct 2017, 11:06 PM
Sergey 23
Sergey 23 - avatar
4 Answers
+ 3
Do variable substitution, that means just plug in the numbers! decor(a, b): def sq(func): return func(a, b) * func(a, b) return sq therefor: decor(2, 3): def sq(func): return func(2, 3) * func(2, 3) return sq So if you say `f = decor(2,3)` you actually have a function f = sq(func): return func(2, 3) * func(2, 3) and now you call f with "add", and we plug in again. sq(add): return add(2, 3) * add(2, 3)
1st Nov 2017, 1:24 AM
Schindlabua
Schindlabua - avatar
+ 3
Here is an example of what a decorator may be useful for: https://code.sololearn.com/cD09ijlIS6Ev/?ref=app
25th Mar 2018, 4:31 PM
Denys Yeromenko
Denys Yeromenko - avatar
+ 1
The example is funny but Decor is not optimal function in this case. https://code.sololearn.com/c9Oz2sFwmouO/?ref=app Decor is useful for local changing enother function.
6th Jan 2018, 6:15 PM
Sergey Konkin
Sergey Konkin - avatar
0
Thank you very much!
1st Nov 2017, 8:19 AM
Sergey 23
Sergey 23 - avatar