鱼C论坛

 找回密码
 立即注册
查看: 4891|回复: 4

[已解决]闭包函数和装饰器

[复制链接]
发表于 2022-11-18 17:41:55 | 显示全部楼层
  1. >>> def outer(func):
  2.     def inner():
  3.         nonlocal func
  4.         func(); func(); func(); func(); func();
  5.         print(f'{func.__name__} 函数被调用了 5 次!')
  6.     return inner

  7. >>> @outer
  8. def temp():
  9.         print('temp 函数被调用了')

  10.        
  11. >>> temp()
  12. temp 函数被调用了
  13. temp 函数被调用了
  14. temp 函数被调用了
  15. temp 函数被调用了
  16. temp 函数被调用了
  17. temp 函数被调用了 5 次!
  18. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-2 04:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表