+ 1
Decorators
text = input() def uppercase_decorator(func): def wrapper(text): print(text.upper()) return wrapper @uppercase_decorator def display_text(text): return(text) print(display_text(text)) Why in the end "None"
1 Respuesta
+ 3
display_text(text) first calls the decorator function with function display_text as argument to it .
Decorator returns the wrapper function . After that wrapper function is called with the argument that is passed from display_text function to it.
wrapper function prints the uppercase value but doesn't returns anything . If it would have returned a value, using print(display_text(text)) would print that returned value but since no value is returned it prints None.
Edit:
The following is how the code works,
a=uppercase_decorator(display_text)
a(text) => prints uppercase value of text but since it returned nothing printing it out ( print(a(text)) ) will return None.