Decorator function

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.

To be honest, this is rather convoluted and confusing example to use of a decorator. Decorator in the introduction is very limited, since, as you noticed, it cannot be used when original function takes arguments - there’s really no reason to limit it that way (other than, maybe, not revealing yet some parts of Python syntax to the campers). For that decorator would need to look like this (notice also names of variables updated to make more sense):

def say_hello():
   name = input('What is your name? ')
   return 'Hello ' + name

def uppercase_decorator(func):
   def wrapper(*args, **kwargs):
       original_result = func(*args, **kwargs)
       modified_result = original_result.upper()
       return modified_result
   return wrapper

new_say_hello = uppercase_decorator(say_hello)

print(new_say_hello())

The main point of the decorator is to return new function, which can be used over and over again. In your version, this part of the functionality wouldn’t be kept, say_hello_res would not be a function, it would be a string with the first name.