+ 1
It works in this way too, but why is it bettwr with decor func?
def wrap(func): print("============") func() print("============") return wrap def print_text(): print("Hello world!") wrap(print_text)
2 ответов
+ 3
def decor(tag):
tag1 = "<"+tag+">"
tag2 = "</"+tag+">"
def wrap(word):
print(tag1+word+tag2)
return wrap
h1 = decor("h1")
p = decor("p")
h1("Title 1")
p("Content of this paragraph")
h1("Title 2")
p("Here is the second paragraph")
# output:
"""
<h1>Title 1</h1>
<p>Content of this paragraph</p>
<h1>Title 2</h1>
<p>Here is the second paragraph</p>
"""
+ 2
it's very useful , if you need to do it the same thing many times
you can use print("============") at the line above and below your code,
but if you use decor function , you can code it easier and faster.