+ 3

Explain Decorators

The function wrap is not called, so how the print function occured!! please explain someone

20th Sep 2016, 6:33 PM
Md Mizanur Rahman
Md Mizanur Rahman - avatar
6 Answers
+ 7
def decor(func): # We create a decorating function that takes another function as a parameter def wrap(): # Nested function that wraps around the function we chose in our parameter print('-'*40) # Creates 40 dashes on top of our function func() # Calls the function we used in our parameter print('-'*40) # Creates 40 dashes below our function return wrap() # Prints out what is in our wrap function @decor # Calls our decor function def text(): # The function that gets called by decor print('decorate me!') # defines text to decorate print(text) # Prints our text function that calls decor
20th Sep 2016, 8:56 PM
vyavas
+ 1
It's all good, I probably could have worded it better haha. Cheers.
21st Sep 2016, 5:56 AM
vyavas
0
It didn't answer my question, everything I understand before what you explained here. Without, wrap function in decor, which is not called. So, how it can be functioning!
21st Sep 2016, 5:01 AM
Md Mizanur Rahman
Md Mizanur Rahman - avatar
0
Ok, got it. With the return, the function is called. Sorry
21st Sep 2016, 5:21 AM
Md Mizanur Rahman
Md Mizanur Rahman - avatar
0
wrap gets assigned to function 'text' using @decor in the above example. So, when we call text, wrap gets called actually.
24th Oct 2016, 3:59 AM
Sravanthi Upadrasta
Sravanthi Upadrasta - avatar
0
Here is an explanation of the decorator usage: https://code.sololearn.com/cD09ijlIS6Ev/?ref=app
7th Jun 2018, 2:02 AM
Denys Yeromenko
Denys Yeromenko - avatar