+ 3
Please, explain me how this decorator works!
from playground https://code.sololearn.com/cs1eRY1k7yRa/?ref=app
4 ответов
+ 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)
+ 3
Here is an example of what a decorator may be useful for:
https://code.sololearn.com/cD09ijlIS6Ev/?ref=app
+ 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.
0
Thank you very much!