https://www.cnblogs.com/linhaifeng/articles/6113086.html
——————————————————————————————————————
十、函数式编程
高阶函数:1.函数接收的参数是一个函数名 2.返回值中包含函数 满足条件之一就是高阶函数
# 把函数当作参数传给另外一个函数
def foo(n): print(n) def bar(name): print('my name is %s' %name) foo(bar('alex'))
运行结果:
my name is alex
None #bar函数没有返回值
#返回值中包含函数
def bar(): print('from bar') def foo(): print('from foo') return bar n=foo() n()
运行结果:
from foo
from bar
def handle(): print('from handle') return handle h=handle() h()
运行结果:
from handle
from handle
#尾调用优化 在函数的最后一步去调用别的函数
#函数bar在foo内为非尾调用
def bar(n):
return n
def foo(x):
y=bar(x)
return y
#函数bar在foo内为非尾调用
def bar(n):
return n
def foo(x):
return bar(x)+1
#函数bar在foo内为尾调用
def bar(n):
return n
def foo(x):
return bar(x)
#函数bar在foo内为尾调用
def bar(n):
return n
def foo(x):
bar(x)