In: Computer Science
Python 3.7:
Write the following decorators and apply them to a single function (applying multiple decorators to a single function):
1. The first decorator is called strong and has an inner
function called wrapper. The purpose of this decorator is to add
the html tags of <strong> and </strong> to the argument
of the decorator. The return value of the wrapper should look like:
return “<strong>” + func() + “</strong>”
2. The decorator will return the wrapper per usual.
3. The second decorator is called emphasis and has an inner
function called wrapper. The purpose of this decorator is to add
the html tags of <em> and </em> to the argument of the
decorator similar to step 1. The return value of the wrapper should
look like: return “<em>” + func() + “</em>.
4. Use the greetings() function in problem 1 as the decorated
function that simply prints “Hello”.
5. Apply both decorators (by @ operator to greetings()).
6. Invoke the greetings() function and capture the result.
'''
Python version : 3.7
decorator.py : Python program to create a decorator functions strong and emphasis
'''
# decorator function strong that takes a function as input.
def strong(func):
# inner function called wrapper with no input argument.
def wrapper():
#adds the html tags <strong> </strong> to the argument of the decorator and returns it
return "<strong>"+func()+"</strong>"
return wrapper
# decorator function emphasis that takes a function as input.
def emphasis(func):
# inner function called wrapper with no input argument.
def wrapper():
#adds the html tags <em> </em> to the argument of the decorator and returns it
return "<em>"+func()+"</em>"
return wrapper
#end of decorator.py
Code Screenshot:
'''
Python version : 3.7
decorator_implement.py : Python program to implement the decorator functions
'''
import decorator
# function greetings that is used to return Hello
@decorator.strong
@decorator.emphasis
def greetings ():
return "Hello"
#end of decorator_implement.py
Code Screenshot:
Output: