+ 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!"))
6 Antworten
+ 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")
+ 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");
+ 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 @?
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.