in Education by
Can someone tell me how to make two decorators that would do the following in python: @makeitalic @makebold def say(): return "Hey" Result should be: "Hey" I just want to understand how decorators works? P.S. - Not trying to make HTML Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You can check the help to see how decorators work. For your Question, check this: from functools import wraps def makeitalic(fn): @wraps(fn) def wrapped(*args, **kwargs): return "" + fn(*args, **kwargs) + "" return wrapped def makebold(fn): @wraps(fn) def wrapped(*args, **kwargs): return "" + fn(*args, **kwargs) + "" return wrapped @makeitalic @makebold def Hey(): return "Hey PC" @makeitalic @makebold def log(s): return s print Hey() # returns "Hey PC" print Hey.__name__ # with functools.wraps() this returns "Hey" print log('Hey') # returns "hello"

Related questions

0 votes
    Can someone please tell me the difference between a function decorated with @staticmethod and one with @classmethod? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to put time delay in the script of Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Is there any shortcut to make a simple list out of a complex one in python? I already performed for a loop but ... help me with it. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I want to output to the screen using print function in Python, How can I do it? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    In a function, How to create and use a global variable? How can I use it in other functions? Is it compulsory ... it in a function? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how can I Call a function in a string with function name in python program? E.x. Let's ... to perform this task. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Is there a built-in that removes duplicates from a list in Python, whilst preserving order? I know that I can ... idiom if possible. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    suppose I have passed : list=[] How can I check if a list is empty or not? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to freeze the dos pop-up, I already tried raw-input as mentioned in my study material of Python 2.6 and ... (3**200) raw_input() Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to use python to connect to a MySQL database, how can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    In Python, How to remove trailing and leading whitespace from str ? For instance check this: " Hey" --> "Hey" " ... "Harsh has a dog" Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How to check which Python version is interpreting my script? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How can I trim whitespace using python, I want to delete all spaces and tabs? For E.x. \t AT\t → At Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
...