闭包

本文详细介绍了Python中的闭包概念及其应用实例,并通过具体案例展示了如何使用闭包实现装饰器模式,增强了代码的复用性和扩展性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

闭包

在一个外函数中定义了一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值室内函数的引用.这样就够成了一个闭包.
一般情况下,在我们认知当中,如果一个函数结束,函数的内部所有东西都会释放掉,还给内存,局部变量都会消失,但是闭包是一种特殊情况,如果外函数在结束的时候发现有自己的临时变量将来会在内部函数中用到,就把这个临时变量绑定给了内部函数,然后自己再结束.
闭包和装饰模式 相辅相成
函数后加()为执行的意思 不加()为传递引用的意思
[python]  view plain  copy
  1.    
  2. #普通闭包  
  3. def outter(fun):  
  4.     def inner():  
  5.         print('123')  
  6.         fun()  
  7.         print('456')  
  8.     return inner  
  9. def index():  
  10.     print('woshi index')  
  11. ind=outter(index)  
  12. ind()  

执行命令得

[python]  view plain  copy
  1.    
  2. 123  
  3. woshi index  
  4. 456  

装饰器闭包

[python]  view plain  copy
  1.    
  2. #装饰器闭包  
  3. def outter(fun):  
  4.     def inner():  
  5.         print('123')  
  6.         fun()  
  7.         print('456')  
  8.     return inner  
  9. @outter  
  10. def index():  
  11.     print('woshi index')  
  12. index()  

执行命令得

[python]  view plain  copy
  1.    
  2. 123  
  3. woshi index  
  4. 456  

练习

火锅点菜结账系统

[python]  view plain  copy
  1.    
  2. def xuebi(fun):  
  3.     def caidan():  
  4.         print('雪碧 5元')  
  5.         return fun()+5  
  6.     return caidan  
  7. def yangrou(fun):  
  8.     def caidan():  
  9.         print('羊肉 80元')  
  10.         return fun()+80  
  11.     return caidan  
  12.  
  13. @xuebi  
  14. @yangrou  
  15. def guodi():  
  16.         print('三鲜锅 50元')  
  17.         return 50  
  18. price=guodi()  
  19. print('总消费',price)  

执行命令得

[python]  view plain  copy
  1.    
  2. 雪碧 5元  
  3. 羊肉 80元  
  4. 三鲜锅 50元  
  5. 总消费 135  
当闭包的内部函数带参数时

1-4分别加9得出结果分别添加到列表中

[python]  view plain  copy
  1.    
  2. list=[]  
  3. def outter():  
  4.     def inner(y):  
  5.         lam=lambda x,y:x+y  
  6.         for x in range(1,5):  
  7.             list.append(lam(x,y))  
  8.     return inner  
  9. out=outter()  
  10. out(9)  
  11. print(list)  

执行命令得

[python]  view plain  copy
  1.    
  2. [10111213]  
当闭包的外部函数带入方法时

1-4分别乘9得出结果添加到列表中

[python]  view plain  copy
  1.    
  2. list=[]  
  3. def outter(fun):  
  4.     def inner(y):  
  5.         for x in range(1,5):  
  6.             list.append(fun(x,y))  
  7.     return inner  
  8. def suan(x,y):  
  9.     return x*y  
  10. out=outter(suan)  
  11. out(9)  
  12. print(list)  

执行命令得

[python]  view plain  copy
  1.    
  2. [9182736]  

Python3特性  str转换为函数eval()实现带参函数fun(x,y)

[python]  view plain  copy
  1.    
  2. x=int(input('请输入第一个数字:'))  
  3. y=int(input('请输入第二个数字:'))  
  4. fun=eval(input('请输入一个函数:'))  
  5. ret=fun(x,y)  
  6. print(ret)  
  7. print(type(fun))  
  8. print(type(ret))  

执行命令得

[python]  view plain  copy
  1.    
  2. 请输入第一个数字:6  
  3. 请输入第二个数字:4  
  4. 请输入一个函数:lambda x,y:x*y  
  5. 24  
  6. <class 'function'>  
  7. <class 'int'>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值