0
I am not abe to understand the working of this code
def decor(func): def wrap(): print("============") func() print("============") return wrap def print_text(): print("Hello world!") decorated = decor(print_text) decorated()
2 Answers
+ 3
it is a decorator.
function decor returns reference to its local function wrap().
function wrap() prints "============" then it calls function that was given as argument to decor(func), after that it prints "============" again.
decorated = decor(print_text)
# decorated now refers to decor.wrap() function
decorated()
# call of decor.wrap() function as decorated = decor.wrap
as func argument of decor was print_text,
decor.wrap() do the next:
print("============")
print_text() # as func refers to print_text
print("============")
output will be:
============
Hello world!
============
+ 1
Thanx sir



