十、python沉淀之路--高阶函数初识

一、高阶函数:分两种:一种是返回值中包含函数体;另一种是把一个函数体当作了参数传给了另一个函数

1、返回值中包含函数体

例1、

1 def test():
2     print('这是一个测试')
3     return test
4 
5 f = test()
6 f()
1 这是一个测试
2 这是一个测试

例2

1 def inward():
2     print('from inward')
3 def outward():
4     print('from outward')
5     return inward
6 
7 f = outward()
8 f()
1 from outward
2 from inward

2、把一个函数体当作一个参数传给另一个函数

例1

1 def inward(name):
2     print('%s is from inward' %name)
3 
4 def outward(n):
5     print('我来自地球以外')
6 
7 outward(inward(''))
1is from inward
2 我来自地球以外
原文地址:https://www.cnblogs.com/jianguo221/p/8965110.html