+ 3
Explain Decorators
The function wrap is not called, so how the print function occured!! please explain someone
6 Respuestas
+ 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
+ 1
It's all good, I probably could have worded it better haha.
Cheers.
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!
0
Ok, got it. With the return, the function is called. Sorry
0
wrap gets assigned to function 'text' using @decor in the above example. So, when we call text, wrap gets called actually.
0
Here is an explanation of the decorator usage:
https://code.sololearn.com/cD09ijlIS6Ev/?ref=app