Python函数闭包与nonlocal关键字

直接从实例出发:假如我们想设计一个求各种水果买多少斤在折扣后多少钱的函数,我们可以这样

def out(price):#单价
    discount=0.9#折扣
    def inn(weight):#重量
        return discount*price*weight
    return inn

apple=out(10)
print(apple(5))
orange=out(15)
print(orange(20))

在这里插入图片描述
这是一个函数嵌套定义,out函数里定义了inn,并且out返回了inn函数(不是返回inn函数调用的结果)

按照表面意思理解,apple接收到的是inn这个内部函数,但是inn用到的discount是out()函数中的变量,而apple用到了out函数的变量,程序却没有报错,说明apple并不是完全独立于外部函数的,在out函数在return inn的时候把discount也一起传回来了,传回来的内部函数和其用到的外部变量就是闭包

函数闭包要满足以下条件

1.必须嵌套函数。

2.内嵌函数必须引用一个定义在闭合范围内(外部函数里)的变量——内部函数引用外部变量

3.外部函数必须返回内嵌函数——必须返回那个内部函数

闭包的作用:可以保持程序上一次运行后的状态然后继续执行。

但是如果我们要修改discount,就会报错


def out(price):
    discount=0.9
    def inn(weight):
        discount*=price*weight#报错,因为discount一旦被赋值,就会被当成局部变量
        return discount
    return inn


apple=out(10)
print(apple(5))
orange=out(15)
print(orange(20))

在这里插入图片描述
discount*=price * weight相当于discount=discount * price * weight
解释器看到disconut= 就会认为要新建一个同名的discount局部变量,但是解释器又看到后面竟然用到了这个局部变量,就会报错在定义前引用

要想解决这个问题,我们可以告诉解释器这个discount不是局部变量,就要用到nonlocal关键字

def out(price):
    discount=0.9
    def inn(weight):
        nonlocal discount
        discount=discount*price*weight#报错,因为discount已经被销毁
        return discount
    return inn

apple=out(10)
print(apple(5))
orange=out(15)
print(orange(20))

在这里插入图片描述
这样就能正常运行

还有一种办法,如果discount是可变数据类型,那么解释器不会把对其切片的赋值其当成局部变量的定义

def out(price):
    discount=[0.9]
    def inn(weight):
        discount[0]=discount[0]*price*weight#报错,因为discount已经被销毁
        return discount[0]
    return inn

也能正常运行
此时闭包包括类型为list的discount

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值