How can I use the decorators in this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
5th Nov 2023, 10:19 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
15 Answers
+ 4
Which decorators? What are they supposed to do in the code?
5th Nov 2023, 10:48 AM
Lisa
Lisa - avatar
+ 4
Decorators are used to modify functions. Which function do you want to modify? Read about decorators: https://pythonbasics.org/decorators/
5th Nov 2023, 11:59 AM
Lisa
Lisa - avatar
+ 1
There is no user defined function in your code. What are the existing input / function, and the expected output? ====== Some Text ====== You example Some Text can be any text. Is it the Exponents Chart, or the printed formula?
6th Nov 2023, 2:47 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Bob_Li I'm already thinking about adding a random function to the decorator. Instead of using * to draw the line, it can randomly picking from other symbols (string.punctuation).
8th Nov 2023, 4:23 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming It is surprising that random is only called once... ok, this works: move the symbol and selected to the inner function also, I used random.choice. it is more suitable for the task. def addtitle(func): def newFunc(args): symbol = string.punctuation selected = random.choice(symbol) ... rest of the code ...
8th Nov 2023, 5:33 AM
Bob_Li
Bob_Li - avatar
+ 1
Wong Hei Ming no need for selected = selected[0] random.choice returns a single item from the list.
8th Nov 2023, 5:54 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li Of course. No point in selecting the first character from a string with a length of one. Forgot to review the code......
8th Nov 2023, 5:58 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Decorator the one we use to add lines designs etc in python like ====== Some text ======
5th Nov 2023, 11:57 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
0
going on a tangent here, but this is nice for superscripts... https://code.sololearn.com/cN6Ngfy6118a/?ref=app
5th Nov 2023, 12:54 PM
Bob_Li
Bob_Li - avatar
0
Exponets Chart
6th Nov 2023, 9:53 AM
P A Arrchith Iyer
P A Arrchith Iyer - avatar
0
P A Arrchith Iyer I guess you are looking for something like this? It is my first time to write a function with decorator, took me 10+ times trial and error. https://code.sololearn.com/c2E5qoJGrEzp/?ref=app
8th Nov 2023, 3:31 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming very nice. yes, decorators with arguments are trickier. it is easier to just directly make the original function fancier
8th Nov 2023, 4:03 AM
Bob_Li
Bob_Li - avatar
0
Other than fancy displays, decorators can also be used to provide extended functionalities for the decorated function. Example for this is the extensive use of decorators in Flask apps. But I sometimes feel we end up building a tall tower of code stacked on top of one another. The dependency can become tightly coupled.
8th Nov 2023, 4:32 AM
Bob_Li
Bob_Li - avatar
0
Bob_Li Your assistance is required. I added the random feature inside the decorator, but the symbol doesn't change on the next runs. I assume following runs call exponents(), and it calls the decorator and select a new symbol again. https://code.sololearn.com/cIEqlOJinZ2Z/?ref=app
8th Nov 2023, 4:49 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Bob_Li Thanks, it works now. Lesson learned. Decorator need a wrapper function to do the "add-on", thus all codes related with adding stuff must be placed inside the wrapper function. https://code.sololearn.com/ck7wSYucdF9e/?ref=app
8th Nov 2023, 5:51 AM
Wong Hei Ming
Wong Hei Ming - avatar