+ 1

Please help me understand those decorating??

Hi all! I don't understand how this works... How does the hello world appear in the middle?? Where is it injected in the middle... I am having trouble understanding how it gets printed in the middle!! Thanks!

1st Feb 2017, 9:10 AM
Greg Isaacson
Greg Isaacson - avatar
5 Respostas
+ 12
If you are talking about this course example below: def decor(func): def wrap(): print("============") func() print("============") return wrap def print_text(): print("Hello world!") decorated = decor(print_text) decorated() decorated() is invoking decor() with print_text as a parameter. What decor() does it prints the double line, invokes the function passed as its parameter and prints the second double line. In this example print_text is a function passed as a parameter to another function. That basically is decorating :)
1st Feb 2017, 6:29 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
This is actually what decorators are for. They alter the behavior/output or return value of a method not by overriding it, but by executing some extra code before and/or after it. The decorator itself takes the method to decorate as parameter.
1st Feb 2017, 8:41 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
Thank you for the clear answer kuba! I read it about 25 times and I understand the idea pretty well now. So as I understand it, what decor does is it runs the first function nested inside of it, then it invokes the decor function of "print text" then the next function triggers off That is nested inside of it? So decor () function nested inside (1) then the function inside of decor... Then the second function nested inside of it?? That's interesting as you are able to make functions run in out of order ways?? So decor goes like this : Define decore()
1st Feb 2017, 7:27 PM
Greg Isaacson
Greg Isaacson - avatar
+ 1
ou'll notice if you run/enter the decor function definition from the example, then try and invoke wrap(), you'll get NameError: name 'wrap' is not defined - Python is as yet completely unaware of the existance of wrap as a callable function. This is because wrap only gets defined when you call decor(func) - wrap doesn't actually get invoked/called by calling decor, it gets defined. Of course, we can't actually call/invoke wrap after decor is defined, because wrap is a local variable within decor and so can't be referrenced after the decor definition is finished. This is why we have to return wrap and assign it to another variable when decor is called (decorated). If this makes sense, we can conclude that the result of the code in the example is that same as writing: def decorated(): print("==========") print("hello") print("==========") The example code given is a little too simplistic to properly demonstrate the power of the decorator function, IMHO. If we perform the simple modification shown below so that we can pass the printed string as an argument to decor, we can then make the decorator function more flexible. def decor(func, str): def wrap(): print("==========") func(str) print("==========") return wrap def print_text(str): print(str) decorated = decor(print_text, "hello") decorated_2 = decor(print_text, "world") decorated() decorated_2() This creates the output: ========== hello ========== ========== world ========== This demonstrates better the power of the decorator function. With just 1 extra line of code we can define the function decorator_2 that prints output of the same format, but changes the text on the 2nd line. Hope that makes things clearer.
16th Feb 2017, 2:09 PM
edd buggins
edd buggins - avatar
+ 1
Made clear understandings Thanx to edd , Kuba and Greg :)
27th Jun 2017, 9:09 AM
S.Naveen Kumar
S.Naveen Kumar - avatar