0
Decorator function not working
Hi there, can someone help why is my decorator function not working? The output of this is ‘None’, but I expect it to be an uppercase of the Input text. text = input() def uppercase_decorator(func): def wrapper(t): func(t.upper()) return wrapper @uppercase_decorator def display_text(text): return(text) print(display_text(text))
6 Réponses
+ 2
Abs Sh is this what u mean? It works now :)
text = input()
def uppercase_decorator(func):
def wrapper(text):
return func(text).upper()
#your code goes here
return wrapper
@uppercase_decorator
def display_text(text):
return(text)
print(display_text(text))
+ 2
You need to return the value because your argument function 'display_text' is returning a value
Look the following:
...
return func(t.upper())
...
print(display_text(text))
...
Just experiment here:
https://code.sololearn.com/cqeQuldcDAEk/?ref=app
I hope this solve your problem.
+ 1
when you call function display_text(text) , it actually calls the wrapper function inside the decorator.
func is the actual display_text function here that you need to print , i.e. print(func(t.upper)).
And since this wrapper doesn't returns anything , using print on it returns None.
0
@abhay iunderstand, but can someone explain why my code doesn’t work as intended?
0
U not returning the func in wrapper
- 1
@ahbag but why is the output of my code None?