Python补充 装饰器 高阶函数 闭包
高阶函数
高阶函数的定义:
1.此函数传入或接收的参数是一个函数名
2.此函数的返回值是一个函数名
3.满足上述两个条件中任意一个,都可称之为高阶函数
#函数名作为参数 def foof(): print('this is func') def test(func): print(foof) test(foof) #函数名作为返回值 def test(): print('test') return test x=test() print(x()) D:/programSoft/PyCharm_pro/projects/pro/func.py test test <function test at 0x00628E40> #函数名作为参数且返回函数名 def foof(): print('func output is foof') def test(func): print('func name is '+func.__name__) return func x=test(foof) x() D:/programSoft/PyCharm_pro/projects/pro/func.py func name is foof func output is foof
装饰器
装饰器本质就是函数,功能是为其他函数添加附加功能。
原则: 不修改被修饰函数的源代码 不修改被修饰函数的调用方式。