Home / Programs / Python Decorators - 4 - Hands On
🚀 Programming Example

Python Decorators - 4 - Hands On

👁 456 Views
💻 Practical Program
📘 Step Learning

Python Decorators - 4 - Hands On

📌 Information & Algorithm

4. Decorators - 4

1. Define a decorator function italic_ tag which adds italic HTML tags function. to the value of another

2. Use the Test against custom input box to output the result for debugging.

3. Provide a string (refer test case sample) to display the output/error.

Given Input:

Hi Decorator

Expected Output:

Hi Decorator

💻 Program Code

import os
import sys


def bold_tag(func):   

    def inner(*args, **kwdargs):
        return '<b>'+func(*args, **kwdargs)+'</b>'
        

    return inner


#Implement italic_tag below
def italic_tag(func):
    def inner(*args, **kwdargs):
        return '<i>'+func(*args, **kwdargs)+'</i>'
    return inner


def say(msg):
    return msg


say=italic_tag(say)

    

'''Check the Tail section for input/output'''

if __name__ == "__main__":
    with open(os.environ['OUTPUT_PATH'], 'w') as fout:
        res_lst = list()
        res_lst.append(say(input()))
        fout.write("{}".format(*res_lst))
                        

🖥 Program Output

<i>Hi Decorator</i>
                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.