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()

3rd Apr 2020, 7:15 AM
Vikas(Vikasrastic)
Vikas(Vikasrastic) - avatar
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! ============
3rd Apr 2020, 8:19 AM
andriy kan
andriy kan - avatar
+ 1
Thanx sir
6th Apr 2020, 8:46 AM
Vikas(Vikasrastic)
Vikas(Vikasrastic) - avatar