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中的urllib2模块
Nov 13 Python
Python WXPY实现微信监控报警功能的代码
Oct 20 Python
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
Mar 13 Python
Python中的十大图像处理工具(小结)
Jun 10 Python
python如何将多个PDF进行合并
Aug 13 Python
python爬虫 基于requests模块发起ajax的get请求实现解析
Aug 20 Python
python3 deque 双向队列创建与使用方法分析
Mar 24 Python
python golang中grpc 使用示例代码详解
Jun 03 Python
keras 指定程序在某块卡上训练实例
Jun 22 Python
jupyter notebook指定启动目录的方法
Mar 02 Python
python 爬取哔哩哔哩up主信息和投稿视频
Jun 07 Python
python实现Nao机器人的单目测距
Sep 04 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根据年月获取季度的方法
2014/03/31 PHP
php快递单号查询接口使用示例
2014/05/05 PHP
Windows2003下php5.4安装配置教程(IIS)
2016/06/30 PHP
php自定义扩展名获取函数示例
2016/12/12 PHP
phpcms的分类名称和类别名称的调用
2017/01/05 PHP
PHP全局使用Laravel辅助函数dd
2019/12/26 PHP
使用jquery实现图文切换效果另加特效
2013/01/20 Javascript
Ext JS 4实现带week(星期)的日期选择控件(实战一)
2013/08/21 Javascript
jQuery多项选项卡的实现思路附样式及代码
2014/06/03 Javascript
JavaScript实现自动对页面上敏感词进行屏蔽的方法
2015/07/27 Javascript
jqPlot jQuery绘图插件的使用
2016/06/18 Javascript
获取select的value、text值的简单示例(jquery与javascript)
2016/12/07 Javascript
jQuery实现可移动选项的左右下拉列表示例
2016/12/26 Javascript
JS简单判断滚动条的滚动方向实现方法
2017/04/28 Javascript
微信小程序new Date()方法失效问题解决方法
2019/07/29 Javascript
vue项目接口管理,所有接口都在apis文件夹中统一管理操作
2020/08/13 Javascript
基于postman获取动态数据过程详解
2020/09/08 Javascript
node脚手架搭建服务器实现token验证的方法
2021/01/20 Javascript
[00:53]2015国际邀请赛 中国区预选赛一触即发
2015/05/14 DOTA
sublime text 3配置使用python操作方法
2017/06/11 Python
Python3使用turtle绘制超立方体图形示例
2018/06/19 Python
对python中数组的del,remove,pop区别详解
2018/11/07 Python
python实现抖音点赞功能
2019/04/07 Python
详解pyppeteer(python版puppeteer)基本使用
2019/06/12 Python
python可视化爬虫界面之天气查询
2019/07/03 Python
python 实现 hive中类似 lateral view explode的功能示例
2020/05/18 Python
丝芙兰香港官网:Sephora香港
2018/03/13 全球购物
药学专业大学生个人的自我评价
2013/11/04 职场文书
教育学习自我评价
2014/02/03 职场文书
防汛工作情况汇报
2014/10/28 职场文书
论文答谢词
2015/01/20 职场文书
车队安全员岗位职责
2015/02/15 职场文书
2015年中秋晚会主持稿
2015/07/30 职场文书
工作计划范文之财务管理
2019/08/09 职场文书
《工作是最好的修行》读后感3篇
2019/12/13 职场文书
Python中tkinter的用户登录管理的实现
2021/04/22 Python