0

Functional decorator usage

Could someone provide an example of functional decorator usage? I.e. Not just enhancing the appearance or readability of code. Thanks in advance!

26th Aug 2018, 7:58 PM
David Foster
David Foster - avatar
2 Respostas
+ 1
Decorators do not only "decorate" - they add functionality to the core function. The simplest example is as follows: def header(): print("Header!") header() ==== output: Header! Now create a decorator: def decor(f): print("<h1>") f() #here we call a decorated function print("</h1>") decor(header) #here we pass our function as decorator argument ==== output: <h1> Header! </h1>
27th Aug 2018, 6:40 AM
strawdog
strawdog - avatar
0
Thanks. So in this case you could create a decorator for each HTML tag to convert a page of code pasting text to make a Web site? It seems an inefficient way to go about it, but I guess this is just and example
27th Aug 2018, 7:56 AM
David Foster
David Foster - avatar