鱼C论坛

 找回密码
 立即注册
查看: 3372|回复: 3

[已解决]函数问题

[复制链接]
发表于 2022-10-25 10:57:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. def add(func):
  2.     def inner():
  3.         x = func()
  4.         return x + 1
  5.     return inner
  6.    
  7. def cube(func):
  8.     def inner():
  9.         x = func()
  10.         return x * x * x
  11.     return inner
  12.    
  13. def square(func):
  14.     def inner():
  15.         x = func()
  16.         return x * x
  17.     return inner
  18.    
  19. @add
  20. @cube
  21. @square
  22. ##def test():
  23. ##    return 2
  24. def test01(a):
  25.     return a
  26.    
  27. print(test01(a=8))
复制代码


请教大牛,为什么我在最后把test()的内容改掉会报错呢?
报错如下:
  1. Traceback (most recent call last):
  2.   File "D:/Program Files/Python37/test/1025-3.py", line 27, in <module>
  3.     print(test01(a=8))
  4. TypeError: inner() got an unexpected keyword argument 'a'
复制代码
最佳答案
2022-10-25 11:04:47
本帖最后由 jackz007 于 2022-10-25 11:20 编辑
  1. def add(func):
  2.     def inner(p):
  3.         x = func(p)          # 调用的是 cube() . inner()
  4.         return x + 1         # 返回 262144 + 1 = 262145
  5.     return inner
  6.    
  7. def cube(func):
  8.     def inner(p):
  9.         x = func(p)          # 调用的是 square() . inner()
  10.         return x * x * x     # 返回 64 * 64 * 64 = 262144
  11.     return inner
  12.    
  13. def square(func):
  14.     def inner(p):
  15.         x = func(p)          #  调用的是 test01()
  16.         return x * x         #  返回 8 * 8 = 64
  17.     return inner
  18. @add
  19. @cube
  20. @square
  21. ##def test():
  22. ##    return 2
  23. def test01(a):
  24.     return a
  25.    
  26. print(test01(8))             # 打印 262145
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-25 11:01:24 | 显示全部楼层
你这函数的语法本来就不对,应该是直接
  1. print(test01(8))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-10-25 11:04:47 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-10-25 11:20 编辑
  1. def add(func):
  2.     def inner(p):
  3.         x = func(p)          # 调用的是 cube() . inner()
  4.         return x + 1         # 返回 262144 + 1 = 262145
  5.     return inner
  6.    
  7. def cube(func):
  8.     def inner(p):
  9.         x = func(p)          # 调用的是 square() . inner()
  10.         return x * x * x     # 返回 64 * 64 * 64 = 262144
  11.     return inner
  12.    
  13. def square(func):
  14.     def inner(p):
  15.         x = func(p)          #  调用的是 test01()
  16.         return x * x         #  返回 8 * 8 = 64
  17.     return inner
  18. @add
  19. @cube
  20. @square
  21. ##def test():
  22. ##    return 2
  23. def test01(a):
  24.     return a
  25.    
  26. print(test01(8))             # 打印 262145
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-10-26 08:13:18 | 显示全部楼层
xiaosi4081 发表于 2022-10-25 11:01
你这函数的语法本来就不对,应该是直接

明白了,谢谢哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 01:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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