0
How can I use the decorators in this code
15 Respostas
+ 4
Which decorators? What are they supposed to do in the code?
+ 4
Decorators are used to modify functions. Which function do you want to modify?
Read about decorators:
https://pythonbasics.org/decorators/
+ 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?
+ 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).
+ 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 ...
+ 1
Wong Hei Ming
no need for
selected = selected[0]
random.choice returns a single item from the list.
+ 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......
0
Decorator the one we use to add lines designs etc in python like
======
Some text
======
0
going on a tangent here, but this is nice for superscripts...
https://code.sololearn.com/cN6Ngfy6118a/?ref=app
0
Exponets Chart
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
0
Wong Hei Ming
very nice.
yes, decorators with arguments are trickier.
it is easier to just directly make the original function fancier
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.
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
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