Python3.5装饰器典型案例分析
作者:loveliuzz 发布时间:2023-03-05 20:32:35
标签:Python3.5,装饰器
本文实例讲述了Python3.5装饰器。分享给大家供大家参考,具体如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#高阶函数+嵌套函数==>装饰器
import time
def timer(func): #timer(test1)-->func=test1
def decor():
start_time = time.time()
func() #run test1
stop_time = time.time()
print("the run time of func is %s" %(stop_time-start_time))
return decor
@timer #test1 = timer(test1)
def test1():
time.sleep(3)
print("in the test1")
@timer #test2 = timer(test2)
def test2():
time.sleep(3)
print("in the test2")
print(timer(test1)) #打印deco的地址
#test1 = timer(test1)
#test2 = timer(test2)
test1() #-->执行decor
test2()
运行结果:
<function timer.<locals>.decor at 0x00B720C0>
in the test1
the run time of func is 3.000171661376953
in the test2
the run time of func is 3.000171661376953
1、装饰器修饰有参数函数
#高阶函数+嵌套函数==>装饰器
import time
def timer(func): #timer(test1)-->func=test1
def decor(arg1,arg2):
start_time = time.time()
func(arg1,arg2) #run test2
stop_time = time.time()
print("the run time of func is %s" %(stop_time-start_time))
return decor
@timer #test2 = timer(test2) = decor test2(name)==>decor(name)
def test2(name,age):
print("test2:",name,age)
test2("liu",23)
运行结果 :
test2: liu 23
the run time of func is 0.0
2、装饰器修饰多个函数,有的函数带参数,有的函数不带参数的情况(采用参数组)
#高阶函数+嵌套函数==>装饰器
import time
def timer(func): #timer(test1)-->func=test1
def decor(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs) #run test1
stop_time = time.time()
print("the run time of func is %s" %(stop_time-start_time))
return decor
@timer #test1 = timer(test1)
def test1():
time.sleep(3)
print("in the test1")
@timer #test2 = timer(test2) = decor test2(name)==>decor(name)
def test2(name,age):
time.sleep(1)
print("test2:",name,age)
#test1 = timer(test1)
#test2 = timer(test2)
test1() #-->执行decor
test2("liu",23)
运行结果:
in the test1
the run time of func is 3.0036065578460693
test2: liu 23
the run time of func is 1.0084023475646973
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/loveliuzz/article/details/77878019


猜你喜欢
- 众所周知,随着数据库体积的日益庞大,其备份文件的大小也水涨船高。虽然说通过差异备份与完全备份配套策略,可以大大的减小SQL Server数据
- 假如文件夹有大量视频文件,需求目标是想从每个视频中提取一帧作为视频的一个封面图片,本文利用opencv-python模块实现需求。结合自己的
- 前言在开始本文之前,先来介绍一下相关内容,大家都知道一些防护SSRF漏洞的代码一般使用正则来判断访问IP是否为内部IP,比如下面这段网上比较
- 之前的博客里使用tf读取数据都是每次fetch一条记录,实际上大部分时候需要fetch到一个batch的小批量数据,在tf中这一操作的明显变
- 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLServer中的加号“+”一样。 比如执行下面
- 本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) 。 MySQ
- 今天做visual transformer研究的时候,发现了einops这么个神兵利器,决定大肆安利一波。先看链接:https://gith
- 当类间继承关系很简单时,super()的使用很简单。class A(object): def __init__(self): p
- 项目中有个定时任务,每天取到一些表数据传到一个外部接口,但是最近总是有异常,今天查了下原因。首先本地和测试环境测试这个程序都没问题,只有线上
- 字符x字符 x\\反斜线字符\0n带有八进制值 0 的字符 n (0 <=
- 这两天一直在做课件,我个人一直不太喜欢PPT这个东西……能不用就不用,我个人特别崇尚极简风。谁让我们是程序员呢,所以就爱上了Jupyter写
- mysql全文搜索,sql的写法: MATCH (col1,col2,…) AGAINST (expr [IN BOOLEAN MODE |
- 多属性排序:把需要排序的属性拿出来作为一个 tuple,主要的放前面,次要的放后面。假如某对象有n个属性,那么先按某规则对属性a进行排序,在
- 最近在做周报的时候,需要把csv文本中的数据提取出来制作表格后生产图表。在获取csv文本内容的时候,基本上都是用with open(file
- 本章的内容完全基于上文的理论基础,实际上一旦理解了索引背后的机制,那么选择高性能的策略就变成了纯粹的推理,并且可以理解这些策略背后的逻辑。示
- 本文实例讲述了Python OS模块。分享给大家供大家参考,具体如下:os模块在自动化测试中,经常需要查找操作文件,比如查找配置文件(从而读
- 本文实例讲述了Python简单I/O操作。分享给大家供大家参考,具体如下:文件:poem = '''hellowor
- 修复Access数据库,我们一般使用微软Office 97中带的Access 97对数据库进行修复和整理。Access数据库被损坏分以下几种
- 目录一、ZIP 格式简介二、浏览器解压方案2.1 定义工具类2.2 在线解压 ZIP 文件三、服务器解压方案3.1 根据文件名解压指定 ZI
- 对于SQL的新手,NULL值的概念常常会造成混淆,他们常认为NULL是与空字符串''相同的事。情况并非如此。例如,下述语句是