隐藏页面特效

有参及无参装饰器的使用

import time def delayed_start(func=None, *, duration=1): # 这一层主要是装饰器参数 def decorator(_func): # 这一层主要是将被装饰器装饰的函数传递进来 def wrapper(*args, **kwargs): # 被装饰器装饰的函数的参数传递进来 time.sleep(duration) return _func(*args, **kwargs) # 真正执行被装饰器装饰的函数 return wrapper if func is None: print(func) return decorator else: print("执行了。。。。。。。") print(func) return decorator(func) """ # 不提供任何参数,使用默认值 @delayed_start def hello(): pass 1.delayed_start被执行,并将hello当成参数传递给func 2.函数执行到判断func时,因为func有值,所以执行decorator(func) 4.直接返回wrapper 5.执行wrapper() 6.返回_func(),真正函数执行的地方,_func() = hello() # 拆分调用顺序 def hello(): pass hello = delayed_start(hello) # 提供可选的关键字参数 @delayed_start(duration=2) def hello(): pass 1.delayed_start被执行,因为有关键字参数duration传递,所以func=None 2.函数执行到判断func时,因为func使用了默认值,所以执行decorator 4.并将hello传递给decorator的参数_func,返回wrapper()函数 5.执行wrapper() 6.返回_func(),真正函数执行的地方,_func() = hello() # 拆分调用顺序 def hello(): pass hello = delayed_start(duration=2)(hello) # 提供括号调用,但不提供任何参数 @delayed_start() def hello(): pass """

__EOF__

本文作者404 Not Found
本文链接https://www.cnblogs.com/weiweivip666/p/16740057.html
关于博主:可能又在睡觉
版权声明:转载请注明出处
声援博主:如果看到我睡觉请喊我去学习
posted @   我在路上回头看  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2020-09-29 celery定时任务
点击右上角即可分享
微信分享提示