Python3.5装饰器典型案例分析


Posted in Python onApril 30, 2019

本文实例讲述了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相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python里大整数相乘相关技巧指南
Sep 12 Python
简单介绍Python中的RSS处理
Apr 13 Python
Go语言基于Socket编写服务器端与客户端通信的实例
Feb 19 Python
通过python顺序修改文件名字的方法
Jul 11 Python
python中的tcp示例详解
Dec 09 Python
在Python 中同一个类两个函数间变量的调用方法
Jan 31 Python
python将字母转化为数字实例方法
Oct 04 Python
window7下的python2.7版本和python3.5版本的opencv-python安装过程
Oct 24 Python
Python 爬取必应壁纸的实例讲解
Feb 24 Python
python等差数列求和公式前 100 项的和实例
Feb 25 Python
详解使用python3.7配置开发钉钉群自定义机器人(2020年新版攻略)
Apr 01 Python
Python基本知识点总结
Apr 07 Python
python如何制作缩略图
Apr 30 #Python
Python3.5装饰器原理及应用实例详解
Apr 30 #Python
11个Python Pandas小技巧让你的工作更高效(附代码实例)
Apr 30 #Python
python制作图片缩略图
Apr 30 #Python
python获取微信企业号打卡数据并生成windows计划任务
Apr 30 #Python
使用Python实现企业微信的自动打卡功能
Apr 30 #Python
Python/Django后端使用PIL Image生成头像缩略图
Apr 30 #Python
You might like
用Flash图形化数据(二)
2006/10/09 PHP
解析php中heredoc的使用方法
2013/06/17 PHP
ThinkPHP模板输出display用法分析
2014/11/26 PHP
php延迟静态绑定实例分析
2015/02/08 PHP
PHP用PDO如何封装简单易用的DB类详解
2017/07/30 PHP
不用ajax实现点击文字即可编辑的方法
2007/12/16 Javascript
7个Javascript地图脚本整理
2009/10/20 Javascript
javascript下判断一个对象是否具有指定名称的属性的的代码
2010/01/11 Javascript
extjs3 combobox取value和text案例详解
2013/02/06 Javascript
js中方法重载如何实现?以及函数的参数问题
2013/08/01 Javascript
一个JavaScript获取元素当前高度的实例
2014/10/29 Javascript
JS+CSS相对定位实现的下拉菜单
2015/10/06 Javascript
JavaScript对象参数的引用传递
2016/01/14 Javascript
javascript数组对象常用api函数小结(连接,插入,删除,反转,排序等)
2016/09/20 Javascript
JS中数组重排序方法
2016/11/11 Javascript
微信小程序url与token设置详解
2017/09/26 Javascript
五步轻松实现JavaScript HTML时钟效果
2020/03/25 Javascript
详解element-ui日期时间选择器的日期格式化问题
2019/04/08 Javascript
element-ui表格合并span-method的实现方法
2019/05/21 Javascript
解决VUE-Router 同一页面第二次进入不刷新的问题
2020/07/22 Javascript
[10:49]2014国际邀请赛 叨叨刀塔第二期为真正的电竞喝彩
2014/07/21 DOTA
python统计字符串中指定字符出现次数的方法
2015/04/04 Python
tensorflow学习教程之文本分类详析
2018/08/07 Python
python 申请内存空间,用于创建多维数组的实例
2019/12/02 Python
Python引入多个模块及包的概念过程解析
2020/09/21 Python
美国最大的无人机经销商:DroneNerds
2018/03/20 全球购物
实习教师自我鉴定
2013/12/12 职场文书
八年级历史教学反思
2014/01/10 职场文书
个人学习群众路线心得体会
2014/11/05 职场文书
淘宝客服专员岗位职责
2015/04/07 职场文书
2015年工商所工作总结
2015/05/21 职场文书
2015年国庆节演讲稿范文
2015/07/30 职场文书
《秋天的怀念》教学反思
2016/02/17 职场文书
人身损害赔偿协议书
2016/03/22 职场文书
某学校的2019年度工作报告范本
2019/10/11 职场文书
Nginx配置根据url参数重定向
2022/04/11 Servers