python学习-41 装饰器 -- 高阶函数

装饰器:本质就是函数。是为其他函数添加附加功能的。


原则:
1.不修改被修饰函数的源代码
2.不修改被修饰函数的调用方式

--- 装饰器的知识储备

装饰器=高阶函数+函数嵌套+闭包

高阶函数

1.高阶函数的定义

····函数接收的参数是一个函数名

····函数的返回值是一个函数名

····满足上述条件任意一个,都可称之为高阶函数

·········

def foo():
    print('hello world')
def test(func):
    print(func)
    func()
test(foo)

运行结果:

<function foo at 0x0000024E9658C2F0>
hello world

Process finished with exit code 0

···········

def foo():

    print('hello world')
def test(func):
    return func

foo =test(foo)
foo()

运行结果:

hello world

Process finished with exit code 0
原文地址:https://www.cnblogs.com/liujinjing521/p/11211778.html