+ 1

Why doesn't decorating work when you pass the input as an argument?

The following prints the input without the decor: def decor(func): def wrap(): print("============") func() print("============") return wrap def print_text(x): print(x) decor(print_text("Hello World!"))

15th May 2020, 6:20 PM
David O'Donovan
David O'Donovan - avatar
6 Respuestas
+ 1
def decor(func): def wrap(y): print("============") func(y) print("============") return wrap def decor1(func): def wrap1(z): print("**************") func(z) print("**************") return wrap1 @decor @decor1 def print_text(x): print(x) print_text("hello, two decors")
16th May 2020, 4:07 AM
guga
guga - avatar
+ 1
But you have the text inside the function when defining it. How does one put it in as an argument: print_text("Any text");
15th May 2020, 6:26 PM
David O'Donovan
David O'Donovan - avatar
+ 1
guga good job you kept the decor and the function and it works perfect. But could you do it without using @decor. Recreate what @decor does without using @?
16th May 2020, 7:22 AM
David O'Donovan
David O'Donovan - avatar
0
Managed to get it without using @ by passing the text as a second argument: def decor(func, x): print("============") func(x) print("============") return decor def print_text(x): return print(x) decor(print_text, "Hello World!") guga your solution is still better. Thank you.
16th May 2020, 7:46 AM
David O'Donovan
David O'Donovan - avatar