I’m going through the Python section of the full-stack developer curriculum. On the introduction to functions here, it has an example for a decorator function that makes the text uppercase:
def say_hello():
name = input('What is your name? ')
return 'Hello ' + name
def uppercase_decorator(func):
def wrapper():
original_func = func()
modified_func = original_func.upper()
return modified_func
return wrapper
say_hello_res = uppercase_decorator(say_hello)
print(say_hello_res())
I understand what it is doing. My question is why use that instead of a simple function like this:
def say_hello():
name = input("What is your name? ")
return "Hello " + name
def uppercase_decorator(func):
original_func = func()
modified_func = original_func.upper()
return modified_func
say_hello_res = uppercase_decorator(say_hello)
print(say_hello_res)
I read this old guide on decorator, and if I understand correctly, the first example is the required syntax for using @decorator so that it can also get the parameters from func (which it doesn’t have in this example). Is that correct?
Thanks in advance.