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!
2 Antworten
+ 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>
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