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 解析html之BeautifulSoup
Jul 07 Python
python实现数通设备端口监控示例
Apr 02 Python
在Python中利用Into包整洁地进行数据迁移的教程
Mar 30 Python
python算法表示概念扫盲教程
Apr 13 Python
Python使用pandas处理CSV文件的实例讲解
Jun 22 Python
Python flask框架如何显示图像到web页面
Jun 03 Python
对Python 字典元素进行删除的方法
Jul 31 Python
基于python requests selenium爬取excel vba过程解析
Aug 12 Python
Pyinstaller打包Scrapy项目的实现步骤
Sep 22 Python
Selenium 安装和简单使用的实现
Dec 04 Python
python实现按日期归档文件
Jan 30 Python
Python实现文字pdf转换图片pdf效果
Apr 03 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
php实现excel中rank函数功能的方法
2015/01/20 PHP
浅析PHP中Session可能会引起并发问题
2015/07/23 PHP
Laravel配合jwt使用的方法实例
2020/10/25 PHP
鼠标移到图片上变大显示而不是放大镜效果
2014/06/15 Javascript
JQuery显示、隐藏div的几种方法简明总结
2015/04/16 Javascript
js实现微信分享代码
2020/10/11 Javascript
CSS中position属性之fixed实现div居中
2015/12/14 Javascript
jQuery动态添加
2016/04/07 Javascript
移动端H5开发 Turn.js实现很棒的翻书效果
2016/06/20 Javascript
探讨跨域请求资源的几种方式(总结)
2016/12/02 Javascript
使用Vue如何写一个双向数据绑定(面试常见)
2018/04/20 Javascript
json字符串传到前台input的方法
2018/08/06 Javascript
小程序websocket心跳库(websocket-heartbeat-miniprogram)
2020/02/23 Javascript
[05:09]2016国际邀请赛中国区预选赛淘汰赛首日精彩回顾
2016/06/29 DOTA
python smtplib模块发送SSL/TLS安全邮件实例
2015/04/08 Python
python检测是文件还是目录的方法
2015/07/03 Python
详解python上传文件和字符到PHP服务器
2017/11/24 Python
使用python生成目录树
2018/03/29 Python
python pandas修改列属性的方法详解
2018/06/09 Python
pyqt5实现登录界面的模板
2020/05/30 Python
Tensorflow: 从checkpoint文件中读取tensor方式
2020/02/10 Python
Python3.7下安装pyqt5的方法步骤(图文)
2020/05/12 Python
Python函数__new__及__init__作用及区别解析
2020/08/31 Python
CSS3实现超酷的黑猫警长首页
2016/04/26 HTML / CSS
css3使用animation属性实现炫酷效果(推荐)
2020/02/04 HTML / CSS
html5的画布canvas——画出弧线、旋转的图形实例代码+效果图
2013/06/09 HTML / CSS
前端实现打印图像功能
2019/08/27 HTML / CSS
初中女生自我鉴定
2013/12/19 职场文书
查环查孕证明
2014/01/10 职场文书
高中学生干部学习的自我评价
2014/02/21 职场文书
乡镇党建工作汇报材料
2014/08/14 职场文书
出售房屋委托书范本
2014/09/24 职场文书
群众路线查摆问题整改措施
2014/10/10 职场文书
小学运动会前导词
2015/07/20 职场文书
mysql字符串截取函数小结
2021/04/05 MySQL
MySQL优化常用的19种有效方法(推荐!)
2022/03/17 MySQL