def logger(msg):
def log_message():
print('Log:', msg)
return log_message # 返回的是函数
log_hi = logger('Hi!') # 此时`log_hi`相当于`log_message`,`Hi`是参数`msg`
log_hi()
def html_tag(tag):
def wrap_text(msg):
print('<{0}>{1}</{0}>'.format(tag, msg))
return wrap_text
print_h1 = html_tag('h1') # 此时`print_h1`相当于`wrap_text`,`h1`相当于参数`tag`
print_h1('Test Headline!') # 此时相当于运行`wrap_text('Test Headline!'),`Test Headline!`相当于参数`msg`
print_h1('Another Headline!')
输出结果:
<h1>Test Headline!</h1>
<h1>Another Headline!</h1>