Day14:函数对象

精髓:可以把函数当成变量去用(func = 内存地址)

def func():
    print('from func')

1. 可以赋值

f = func
print(f,func)
f()

2. 可以当作函数的参数传给另一个函数

def foo(x):
    # print(x)
    x()


foo(func) # 把func的内存地址给了x

3. 可以当作函数,当作另一个函数的返回值

def foo(x):
    return x


res = foo(func)
print(res)


res()

4. 可以当作容器类型的一个元素

l = {func,}
print(l)
l[0]() # 先调用func函数的内存地址,在调用函数

猜你喜欢

转载自blog.csdn.net/weixin_64809364/article/details/134348378
今日推荐