大家好,我是 bug,一个普通程序员。
程序员的快速成长,在于多练习,多输出,多分享,多链接。
然而,执行力太差了,我错过了很多好机会。[En]
However, the execution was so poor that I missed a lot of good opportunities.
开始永远不会太晚。欢迎您的关注,见证一个普通程序员的成长。
[En]
It is never too late to start. Welcome your attention and witness the growth of an ordinary programmer.
这里先立个 Flag,参加 涛哥聊Python技术圈 知识星球的每日作业打卡,先坚持 7 天。
问题:
Day1
Q15: 写一个记录函数执行时间的装饰器。
解决:
要完成今天的作业,对我来说有两道难题:
[En]
To finish today’s homework, there are two difficult problems for me:
其一:Python 的开发环境。
其二:身为 Python 初学者,不太理解什么是装饰器,需要研究下。
解决第一个问题,Python 开发环境。
百度关键词,windows + python 环境配置。
你可以准确地找到很多文章,都是相当详细的。
[En]
You can accurately find a lot of articles, all of which are quite detailed.
找到新的生产日期并按照逐步配置轻松完成。
[En]
Find a new production date and follow the step-by-step configuration to do it easily.
Python 用的 3.10 的版本,开发工具用的 PyCharm,没什么特殊配置,省事。
第二个问题,Python 装饰器。
同样先百度,有条件可以 Google。
找到相关文章后,阅读并学习。
[En]
After finding the relevant articles, read and learn.
学习了 python 函数的常见用法后,轻松将 demo 写完。
修饰符函数定义一个修饰符函数,另一个函数用该修饰符函数进行修饰。当该函数被执行时,相应的修饰符函数中的函数也被执行。
[En]
The decorator function defines one decorator function, and the other function is decorated with this decorator function. When this function is executed, the functions in the corresponding decorator function are also executed.
通过 @装饰器函数名 的调用方式实现。
代码如下:
import time
def func_decorator_execute_and_execution_time_report(func):
def wrap_the_func():
before_func_time = time.time()
func()
after_func_time = time.time()
execution_time = after_func_time - before_func_time
print("func execution time: " + str(execution_time))
return wrap_the_func
@func_decorator_execute_and_execution_time_report
def hello_bug():
# 函数暂停 2 秒再执行
time.sleep(2)
print("hello bug")
hello_bug()
运行结果:
hello bug
func execution time: 2.004734992980957
总结:
好了,Day1 打卡结束,学到了 python 函数的常见用法,以及装饰器函数的定义和使用。
如果你也正在学习 Python ,欢迎和 bug 一起交流。
关注公众号 bug 玩编程,一起玩编程!Original: https://www.cnblogs.com/autismbug/p/15428618.html
Author: bug玩编程
Title: 【Python每日作业】Day1: 写一个记录函数执行时间的装饰器
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/511072/
转载文章受原作者版权保护。转载请注明原作者出处!