鱼C论坛

 找回密码
 立即注册
查看: 1909|回复: 9

[已解决]修饰器问题

[复制链接]
发表于 2020-10-24 10:32:17 | 显示全部楼层 |阅读模式

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

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

x
  1. import time

  2. def cac(func):
  3.     def inner(*args, **kwargs):
  4.         time1 = time.time()
  5.         print(
  6.             f'args:{args}'
  7.             
  8.             # f'*args:{*args}'
  9.             
  10.             f' kwargs:{kwargs}'

  11.             # f'**kwargs:{**kwargs}'
  12.         )
  13.         func(*args, **kwargs)
  14.         time2 = time.time()
  15.         print(time2 - time1)
  16.     return inner

  17. @cac
  18. def a(key, value, c=1, b=2):
  19.     pass


  20. a(1, 2)
复制代码


kwargs的输出结果,为什么是空的字典呢?

  1. args:(1, 2) kwargs:{}
  2. 2.2649765014648438e-05
复制代码
最佳答案
2020-10-24 12:44:24
本帖最后由 suchocolate 于 2020-10-24 13:13 编辑

你没传,要这样:
  1. a(1, 2, c=8, b=9)
复制代码


另外建议定义a的时候也改成变长参数,保持一致性,还能避免传多了其他参数:
  1. import time

  2. def cac(func):
  3.     def inner(*args, **kwargs):
  4.         time1 = time.time()
  5.         print(f'args:{args}', f'kwargs:{kwargs}')
  6.         func(*args, **kwargs)
  7.         time2 = time.time()
  8.         print(time2 - time1)
  9.     return inner

  10. @cac
  11. def a(*args, **kwargs):
  12.     pass


  13. a(1, 2, c=8, b=9, d=10)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-24 10:34:56 | 显示全部楼层

回帖奖励 +10 鱼币

鱼币
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-24 10:35:41 | 显示全部楼层
谢谢你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-24 12:44:24 | 显示全部楼层    本楼为最佳答案   
本帖最后由 suchocolate 于 2020-10-24 13:13 编辑

你没传,要这样:
  1. a(1, 2, c=8, b=9)
复制代码


另外建议定义a的时候也改成变长参数,保持一致性,还能避免传多了其他参数:
  1. import time

  2. def cac(func):
  3.     def inner(*args, **kwargs):
  4.         time1 = time.time()
  5.         print(f'args:{args}', f'kwargs:{kwargs}')
  6.         func(*args, **kwargs)
  7.         time2 = time.time()
  8.         print(time2 - time1)
  9.     return inner

  10. @cac
  11. def a(*args, **kwargs):
  12.     pass


  13. a(1, 2, c=8, b=9, d=10)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-24 13:08:23 | 显示全部楼层
suchocolate 发表于 2020-10-24 12:44
你没传,要这样:

另外建议定义a的时候也改成变长参数,保持一致性,还能避免传多了其他参数:

我想问下

  1. def inner(*args, **kwargs):
复制代码


这个inner函数,内部还有func函数

  1. func(*args, **kwargs)
复制代码


kwargs不是字典吗,我如果打印**kwargs,会报错,为什么func函数传入的是**kwargs呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-24 13:20:03 | 显示全部楼层

我这没报错
  1. import time

  2. def cac(func):
  3.     def inner(*args, **kwargs):
  4.         time1 = time.time()
  5.         print(f'args:{args}', f'kwargs:{kwargs}')
  6.         func(*args, **kwargs)
  7.         time2 = time.time()
  8.         print(time2 - time1)
  9.     return inner

  10. @cac
  11. def a(*args, **kwargs):
  12.     print(kwargs)


  13. a(1, 2, c=8, b=9, d=10)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-24 13:52:06 | 显示全部楼层
  1. import time

  2. def cac(func):
  3.     def inner(*args, **kwargs):
  4.         time1 = time.time()
  5.         print(f'args:{*args}', f'kwargs:{**kwargs}')
  6.         func(*args, **kwargs)
  7.         time2 = time.time()
  8.         print(time2 - time1)
  9.     return inner

  10. @cac
  11. def a(*args, **kwargs):
  12.     print(kwargs)


  13. a(1, 2, c=8, b=9, d=10)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-24 13:52:54 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-24 14:30:02 | 显示全部楼层
本帖最后由 suchocolate 于 2020-10-24 14:34 编辑


*和**在函数参数定义里代表接收元组和字典,你那样用没有其他元素给他赋值,语句不正确。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-24 14:45:31 | 显示全部楼层
suchocolate 发表于 2020-10-24 14:30
*和**在函数参数定义里代表接收元组和字典,你那样用没有其他元素给他赋值,语句不正确。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 18:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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