Python 高阶函数

高阶函数:

1.函数名可以作为参数传入
2.函数名可以作为返回值
3.函数名可以进行赋值

def f(n):
    return n * n


def foo(x, y, func):
    return func(x) + func(y)

print(foo(1, 2, f))                 # 函数 f 作为参数传入

运行结果:
5
def f():
    def inner():
        return 6
    print(inner)
    return inner                    # 函数名作为返回值

rest = f()
print(rest)
print(rest())

运行结果:
<function f.<locals>.inner at 0x00000137580B01E0>
<function f.<locals>.inner at 0x00000137580B01E0>
6
原文地址:https://www.cnblogs.com/klvchen/p/8743904.html