In: Computer Science
Write a decorator function that has the following property:
1. The name of a decorator is uppercase and takes a function as input.
2. The decorator has an inner function called wrapper with no input argument.
3. The wrapper function has two local variables: original and modified.
4. The input function argument of decorator gets assigned to the original variable inside the wrapper function and the upper case version of the input function gets assigned to the modified variable.
5. The wrapper function returns the modified variable.
6. The decorator returns the wrapper (per the usual of the inner functions in decorators)
7. Write a new function called greetings that prints “Hello”.
8. Decorate the greetings function with the uppercase decorator function.
9. Run the program by invoking the greetings function. Record the output and include it with your code.
10. Save the decorator uppercase as a separate Python file and import it to a new file that defines the greetings function as a decorated function that simply prints the “Hello” message.
11. Generate the output by invoking the greetings function.
'''
Python version : 2.7
uppercase_decorator.py : Python program to create a decorator function uppercase
'''
# decorator function uppercase that takes a function as input.
def uppercase(fun):
# inner function called wrapper with no input argument.
def wrapper():
# original assigned to the input function argumnet
original = fun()
# uppercase version of the function gets assigned to modified
modified = original.upper()
# return the modified variable
return modified
# return the wrapper
return wrapper
#end of program
Code Screenshot:
'''
Python version : 2.7
uppercase_implement.py : Python program to implement the decorator function
'''
from uppercase_decorator import uppercase
# function greetings that is used to print hello
@uppercase
def greetings ():
return "Hello!"
#call the greetings function
greetings()
#end of program
Code Screenshot:
Output: