鱼C论坛

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

[已解决]装饰器流程判断

[复制链接]
发表于 2022-11-22 10:39:58 | 显示全部楼层 |阅读模式

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

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

x
现有代码如下
  1. @fun_d(minNum=10,maxNum=20)
  2. def fun1(a,b):
  3.     return a+b

  4. @fun_d(minNum=10,maxNum=20)
  5. def fun2(a,b,c):
  6.     return a+b-c
复制代码


要求实现 fun_d 装饰器,要求:
确保使用该装饰器的方法参数都是int类型,否则抛出异常
(提示可以使用 isinstance(i,int) 判断i是否是int 类型)

参数的值在minNum 和 maxNum之间
如果参数值不符合,抛出异常
最佳答案
2022-11-22 10:57:43
  1. def fun_d(minNum, maxNum):
  2.     def outer(func):
  3.         nonlocal minNum, maxNum
  4.         def inner(*args):
  5.             nonlocal minNum, maxNum, func
  6.             for i in args:
  7.                 assert isinstance(i, int)
  8.                 assert minNum <= i <= maxNum
  9.             return func(*args)
  10.         return inner
  11.     return outer

  12. @fun_d(minNum=10,maxNum=20)
  13. def fun1(a,b):
  14.     return a+b

  15. fun1(10, 20)
  16. 30
  17. fun1(1, 2)
  18. Traceback (most recent call last):
  19.   File "<pyshell#33>", line 1, in <module>
  20.     fun1(1, 2)
  21.   File "<pyshell#29>", line 8, in inner
  22.     assert minNum <= i <= maxNum
  23. AssertionError
  24. fun1('1', '2')
  25. Traceback (most recent call last):
  26.   File "<pyshell#34>", line 1, in <module>
  27.     fun1('1', '2')
  28.   File "<pyshell#29>", line 7, in inner
  29.     assert isinstance(i, int)
  30. AssertionError
  31. @fun_d(minNum=10,maxNum=20)
  32. def fun2(a,b,c):
  33.     return a+b-c

  34. fun2(1, 2, 3)
  35. Traceback (most recent call last):
  36.   File "<pyshell#37>", line 1, in <module>
  37.     fun2(1, 2, 3)
  38.   File "<pyshell#29>", line 8, in inner
  39.     assert minNum <= i <= maxNum
  40. AssertionError
  41. fun2('1', '2', '3')
  42. Traceback (most recent call last):
  43.   File "<pyshell#38>", line 1, in <module>
  44.     fun2('1', '2', '3')
  45.   File "<pyshell#29>", line 7, in inner
  46.     assert isinstance(i, int)
  47. AssertionError
  48. fun2(10, 15, 20)
  49. 5
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-22 10:57:43 | 显示全部楼层    本楼为最佳答案   
  1. def fun_d(minNum, maxNum):
  2.     def outer(func):
  3.         nonlocal minNum, maxNum
  4.         def inner(*args):
  5.             nonlocal minNum, maxNum, func
  6.             for i in args:
  7.                 assert isinstance(i, int)
  8.                 assert minNum <= i <= maxNum
  9.             return func(*args)
  10.         return inner
  11.     return outer

  12. @fun_d(minNum=10,maxNum=20)
  13. def fun1(a,b):
  14.     return a+b

  15. fun1(10, 20)
  16. 30
  17. fun1(1, 2)
  18. Traceback (most recent call last):
  19.   File "<pyshell#33>", line 1, in <module>
  20.     fun1(1, 2)
  21.   File "<pyshell#29>", line 8, in inner
  22.     assert minNum <= i <= maxNum
  23. AssertionError
  24. fun1('1', '2')
  25. Traceback (most recent call last):
  26.   File "<pyshell#34>", line 1, in <module>
  27.     fun1('1', '2')
  28.   File "<pyshell#29>", line 7, in inner
  29.     assert isinstance(i, int)
  30. AssertionError
  31. @fun_d(minNum=10,maxNum=20)
  32. def fun2(a,b,c):
  33.     return a+b-c

  34. fun2(1, 2, 3)
  35. Traceback (most recent call last):
  36.   File "<pyshell#37>", line 1, in <module>
  37.     fun2(1, 2, 3)
  38.   File "<pyshell#29>", line 8, in inner
  39.     assert minNum <= i <= maxNum
  40. AssertionError
  41. fun2('1', '2', '3')
  42. Traceback (most recent call last):
  43.   File "<pyshell#38>", line 1, in <module>
  44.     fun2('1', '2', '3')
  45.   File "<pyshell#29>", line 7, in inner
  46.     assert isinstance(i, int)
  47. AssertionError
  48. fun2(10, 15, 20)
  49. 5
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-22 11:53:25 | 显示全部楼层
  1. def fun_d(minNum, maxNum):
  2.     def outer(func):
  3.         nonlocal minNum, maxNum
  4.         def inner(*args):
  5.             nonlocal minNum, maxNum, func
  6.             for i in args:
  7.                 assert isinstance(i, int)
  8.                 assert minNum <= i <= maxNum
  9.             return func(*args)
  10.         return inner
  11.     return outer

  12. @fun_d(minNum=10,maxNum=20)
  13. def fun1(a,b):
  14.     return a+b

  15. @fun_d(minNum=10,maxNum=20)
  16. def fun2(a,b,c):
  17.     return a+b-c

  18. f = fun1(10,20),fun2(10,20,15)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-22 11:57:59 | 显示全部楼层
  1. def fun_d(minNum = 10 , maxNum = 20):
  2.     def outer(func):
  3.         def inner(* args):
  4.             for x in args :
  5.                 if not type(x) == int or x < minNum or x > maxNum :
  6.                     return str(args) + ' : parameter type or value error.'
  7.             else :
  8.                 return func(* args)
  9.         return inner
  10.     return outer

  11. @fun_d(10 , 20)
  12. def fun1(a,b):
  13.     return a+b

  14. @fun_d(10 , 20)
  15. def fun2(a,b,c):
  16.     return a+b-c

  17. print(fun1(15 , 16))               # 合格
  18. print(fun1(3 , 15))                # 不合格
  19. print(fun2(12 , 13 , 15))          # 合格
  20. print(fun2(2 , 3 , 5))             # 不合格
  21. print(fun1('abc' , 123))           # 不合格
  22. print(fun2('abc' , 'bcd' , 123))   # 不合格
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-22 12:05:44 | 显示全部楼层


看起来确实更简洁一些,手动最佳~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 18:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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