Python装饰器的练习题
作者:python学习者0 时间:2023-12-07 13:11:48
1.请实现一个装饰器,把函数的返回值+100然后返回
def wapper(func):
def innner(*args,**kwargs):
ret=func(*args,**kwargs)
ret=print(ret+100)
return ret
return innner
@wapper
def func(number):
return int(number)
func(100)
###结果:200
2.请实现一个装饰器,通过一次调用使函数重复执行5次
#Python学习交流群:725638078
def wapper(func):
def innner(*args,**kwargs):
count=0
while count<5:
func(*args,**kwargs)
count+=1
return innner
@wapper
def func():
print("执行")
func()
3.请实现一个装饰器每次调用函数时,将函数名字及调用函数的时间点写入文件中
import time
def wapper(func):
def inner(*args,**kwargs):
with open("log",encoding="utf-8",mode="a+") as f:
structime=time.localtime()
f.write(f'北京时间:{time.strftime("%Y-%m-%d %H:%M:%S",structime)} 函数名字为:{func.__name__}\n')
ret=func(*args,**kwargs)
return ret
return inner
@wapper
def func():
print("执行")
func()
结尾给大家推荐一个非常好的学习教程,希望对你学习Python有帮助!
来源:https://www.cnblogs.com/xxpythonxx/p/15574404.html
标签:Python,装饰器,练习题
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python PyQt5实现的简易计算器功能示例
2022-01-22 12:03:19
![](https://img.aspxhome.com/file/2023/0/127540_0s.png)
PyTorch的torch.cat用法
2023-11-26 08:57:32
![](https://img.aspxhome.com/file/2023/9/131439_0s.jpg)
python实现人像动漫化的示例代码
2021-08-16 06:36:17
![](https://img.aspxhome.com/file/2023/7/130977_0s.jpg)
手把手教你pycharm专业版安装破解教程(linux版)
2023-03-03 08:58:56
![](https://img.aspxhome.com/file/2023/3/97513_0s.png)
利用bootstrapValidator验证UEditor
2024-04-16 08:47:26
![](https://img.aspxhome.com/file/2023/9/137019_0s.png)
关于Flask 视图介绍
2022-10-21 15:07:34
![](https://img.aspxhome.com/file/2023/2/85082_0s.png)
Python的 元组(Tuple)详解
2021-09-07 13:55:18
![](https://img.aspxhome.com/file/2023/4/120334_0s.png)
如何使用Python程序完成描述性统计分析需求
2021-01-11 22:11:20
![](https://img.aspxhome.com/file/2023/1/105261_0s.png)
php投票系统之增加与删除投票(管理员篇)
2023-10-14 09:44:53
![](https://img.aspxhome.com/file/2023/0/55710_0s.png)
关于反爬虫的一些简单总结
2022-09-21 18:38:59
![](https://img.aspxhome.com/file/2023/7/109537_0s.jpg)
Javascript中作用域的详细介绍
2024-04-18 10:02:09
opera img onload重复执行
2008-01-17 12:01:00
python登录豆瓣并发帖的方法
2021-03-06 14:48:35
openstack中的rpc远程调用的方法
2022-09-23 03:45:17
![](https://img.aspxhome.com/file/2023/3/97493_0s.png)
Python实现前向和反向自动微分的示例代码
2022-10-25 15:52:03
![](https://img.aspxhome.com/file/2023/4/115644_0s.png)
在Vue-cli里应用Vuex的state和mutations方法
2024-04-29 13:09:51
Python实现完整的事务操作示例
2023-11-15 21:02:02
Python必考的5道面试题集合
2021-07-11 19:38:41
![](https://img.aspxhome.com/file/2023/8/84078_0s.png)
利用Python函数实现一个万历表完整示例
2022-06-30 18:06:22
Django多进程滚动日志问题解决方案
2023-02-01 16:21:16