I'm learning the concepts of first class functions and closures in Python and I was curious to know:
Given a higher-order function:
def html_tag(tag):
def wrap_text(text):
print("<{0}>{1}</{0}>".format(tag, text))
return wrap_text
- What's the difference between these two approaches of executing higher-order functions.
- Are there any advantages of using one over the other.
- What is the best way to execute a higher-order function in a program.
1.
print_h1 = html_tag('h1')
print_h1('Test Headline')
print_h1('Another Headline')
2.
html_tag('h1')('Test Headline')
html_tag('h1')('Another Headline')