22、Python之函数对象和闭包

一、函数对象

可以把函数作为变量去使用,不加括号作为内存地址,加括号作为函数运行作为返回值

1.1 可以被赋值

# func=内存地址
def func():
    print('from func')

f=func
print(f,func)  # '<function func at 0x0000027B07F36280> <function func at 0x0000027B07F36280>'
f()  # from func

1.2 函数可以当作参数传给另一个函数

# func=内存地址
def func():
    print('from func')

def foo(x): # 若x = func的内存地址
    print(x)
    # x()

foo(func) # 则返回为foo(func的内存地址) <function func at 0x0000027B07F36280>

def foo(x): # x = func的内存地址
    # print(x)
    x()  # 内存地址(函数名)加括号,即作为函数运行

foo(func) # from func

1.3 函数可以当作一个函数的返回值

# func=内存地址
def func():
    print('from func')

def foo(x): # x=func的内存地址
    return x # return func的内存地址

res=foo(func) # foo(func的内存地址)
print(res) # res=func的内存地址

res()

1.4 函数可以当作容器类型的一个元素

# func=内存地址
def func():
    print('from func')

l=[func,]
# print(l)  # [<function func at 0x0000027A45EA6280>]
l[0]()  # from func

dic={'k1':func} 
print(dic)  # {'k1': <function func at 0x0000027A45EA6280>}
dic['k1']()  # from func

二、函数嵌套

2.1 函数的嵌套调用:在调用一个函数的过程中又调用其他函数

def max2(x,y):
    if x > y:
        return x
    else:
        return y


def max4(a,b,c,d):
    # 第一步:比较a,b得到res1
    res1=max2(a,b)
    # 第二步:比较res1,c得到res2
    res2=max2(res1,c)
    # 第三步:比较res2,d得到res3
    res3=max2(res2,d)
    return res3

res=max4(1,2,3,4)
print(res)  # 4

2.2 函数的嵌套定义:在函数内定义其他函数

def f1():
    def f2():
        pass
# 圆形
# 求圆形的求周长:2*pi*radius
def circle(radius,action=0):
    from math import pi

    def perimiter(radius):
        return 2*pi*radius

    # 求圆形的求面积:pi*(radius**2)
    def area(radius):
        return pi*(radius**2)

    if action == 0:
        return 2*pi*radius

    elif action == 1:
        return area(radius)

res=circle(33,action=0)
print(res)  # 207.34511513692635

三、闭包函数

闭包函数=名称空间与作用域+函数嵌套+函数对象
核心点:名字的查找关系是以函数定义阶段为准

3.1 什么是闭包函数

# "闭"函数指的该函数是内嵌函数
# "包"函数指的该函数包含对外层函数作用域名字的引用(不是对全局作用域)
# 闭包函数:名称空间与作用域的应用+函数嵌套
def f1():
    x = 12306
    def f2():
        print(x)
    f2()    # f2()是内嵌函数,但是又有对外层函数作用域名字的引用
#
#
x=11111
def bar():
    x=444444
    f1()

def foo():
    x=2222
    bar()

foo()  # 12306,运行foo(),调用bar(),运行bar(),调用f1(),运行f1(),即运行f2(),由于f1中已固化x,因此运行结果与外界x无关
# 闭包函数:函数对象
def f1():
    x = 33333333333333333333
    def f2():
        print('函数f2:',x)
    return f2  # 返回的是f2的内存地址,加括号就是返回值了

f=f1()  # f2的内存地址,与f绑定了
print(f)  # <function f1.<locals>.f2 at 0x0000021496203040>

x=4444
f()  # 函数f2: 33333333333333333333
def foo():
    x=5555
    f()

foo()  # 函数f2: 33333333333333333333

3.2 闭包函数的应用

# 两种为函数体传参的方式:
# 方式一:直接把函数体需要的参数定义成形参
def f2(x):
    print(x)

f2(1)
f2(2)
f2(3)

# 方式二:将值包给函数
def f1(x): # x=3
    x=3
    def f2():
        print(x)
    return f2

x=f1(2)  # 函数f1()中括号内参数的值无任何意义,不影响最终结果,定义函数时,x的值已被固化
print(x)  # <function f1.<locals>.f2 at 0x0000022763134040>

x()  # 3
import requests

# 传参的方案一:
def get(url):
    response=requests.get(url)
    print(len(response.text))

get('https://www.baidu.com')

get('https://zhuanlan.zhihu.com/p/109056932')


# 传参的方案二:
def outter(url):
    # url='https://www.baidu.com'
    def get():
        response=requests.get(url)
        print(len(response.text))
    return get

baidu=outter('https://www.baidu.com')
baidu()

zhihu=outter('https://zhuanlan.zhihu.com/p/109056932')
zhihu()

猜你喜欢

转载自www.cnblogs.com/zuiyouyingde/p/12536400.html