How to make a chain of function decorators

0 votes

How can I make two decorators in Python that would do the following?

@makebold
@makeitalic
def say():
   return "Hello"

...which should return:

"<b><i>Hello</i></b>"

I'm not trying to make HTML this way in a real application - just trying to understand how decorators and decorator chaining works.

Apr 13, 2020 in Python by kartik
• 37,520 points
1,201 views

1 answer to this question.

0 votes

Hello @kartik,

Python decorators add extra functionality to another function

An italics decorator could be like

def makeitalic(fn):
    def newFunc():
        return "<i>" + fn() + "</i>"
    return newFunc

Note that a function is defined inside a function. What it basically does is replace a function with the newly defined one. For example, I have this class

class foo:
    def bar(self):
        print "hi"
    def foobar(self):
        print "hi again"

Now say, I want both functions to print "---" after and before they are done. I could add a print "---" before and after each print statement. But because I don't like repeating myself, I will make a decorator

def addDashes(fn): # notice it takes a function as an argument
    def newFunction(self): # define a new function
        print "---"
        fn(self) # call the original function
        print "---"
    return newFunction
    # Return the newly defined function - it will "replace" the original

So now I can change my class to

class foo:
    @addDashes
    def bar(self):
        print "hi"

    @addDashes
    def foobar(self):
        print "hi again"

Hope this works!!

answered Apr 13, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer

How to make function decorators and chain them together?

Here is what you asked for: from functools ...READ MORE

answered Dec 4, 2020 in Python by Gitika
• 65,770 points
574 views
+1 vote
4 answers

how to sort a list of numbers without using built-in functions like min, max or any other function?

Yes it is possible. You can refer ...READ MORE

answered Jun 27, 2019 in Python by Arvind
• 3,050 points
188,466 views
0 votes
1 answer

How to make a password validator without the use of the any()?

Hi, @There, Regarding your query I would suggest ...READ MORE

answered Dec 8, 2020 in Python by Gitika
• 65,770 points
624 views
+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
4,906 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,435 views
0 votes
1 answer
0 votes
1 answer

How to make a flat list out of list of lists?

Hii @kartik, Given a list of lists l, flat_list = ...READ MORE

answered Apr 13, 2020 in Python by Niroj
• 82,840 points
657 views
0 votes
1 answer

How to make a flat list out of list of lists?

Hello @kartik, You can use itertools.chain(): import itertools list2d = [[1,2,3], ...READ MORE

answered Jun 5, 2020 in Python by Niroj
• 82,840 points
1,078 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP