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实现搜索本地文件信息写入文件的方法
Feb 22 Python
Python使用tablib生成excel文件的简单实现方法
Mar 16 Python
python读取excel表格生成erlang数据
Aug 26 Python
详解用Python处理HTML转义字符的5种方式
Dec 27 Python
python中的随机函数random的用法示例
Jan 27 Python
python3 爬取图片的实例代码
Nov 06 Python
在numpy矩阵中令小于0的元素改为0的实例
Jan 26 Python
python的schedule定时任务模块二次封装方法
Feb 19 Python
python实现输入任意一个大写字母生成金字塔的示例
Oct 27 Python
Python中顺序表原理与实现方法详解
Dec 03 Python
详解python环境安装selenium和手动下载安装selenium的方法
Mar 17 Python
基于打开pycharm有带图片md文件卡死问题的解决
Apr 24 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下通过伪造http头破解防盗链的代码
2010/07/03 PHP
PHP图像处理类库及演示分享
2015/05/17 PHP
JS类库Bindows1.3中的内存释放方式分析
2007/03/08 Javascript
JS获取dom 对象 ajax操作 读写cookie函数
2009/11/18 Javascript
Dom 是什么的详细说明
2010/10/25 Javascript
JS实现让网页背景图片斜向移动的方法
2015/02/25 Javascript
JS实现横向与竖向两个选项卡Tab联动的方法
2015/09/27 Javascript
纯JS单页面赛车游戏制作代码分享
2017/03/03 Javascript
javascript基本常用排序算法解析
2017/09/27 Javascript
通过一个简单的例子学会vuex与模块化
2017/11/22 Javascript
微信小程序支付及退款流程详解
2017/11/30 Javascript
js阻止默认右键的下拉菜单方法
2018/01/02 Javascript
基于 D3.js 绘制动态进度条的实例详解
2018/02/26 Javascript
vue在手机中通过本机IP地址访问webApp的方法
2018/08/15 Javascript
JQuery中queue方法用法示例
2019/01/31 jQuery
小程序实现多列选择器
2019/02/15 Javascript
JS控制下拉列表左右选择实例代码
2020/05/08 Javascript
详解微信小程序动画Animation执行过程
2020/09/23 Javascript
jQuery实现简单弹幕制作
2020/12/10 jQuery
Python简单生成随机姓名的方法示例
2017/12/27 Python
Python实现计算对象的内存大小示例
2019/07/10 Python
基于python实现的百度新歌榜、热歌榜下载器(附代码)
2019/08/05 Python
Django中使用MySQL5.5的教程
2019/12/18 Python
keras实现调用自己训练的模型,并去掉全连接层
2020/06/09 Python
使用gunicorn部署django项目的问题
2020/12/30 Python
Python中的流程控制详解
2021/02/18 Python
CSS3+font字体文件实现圆形半透明菜单具体步骤(图解)
2013/06/03 HTML / CSS
Sephora丝芙兰菲律宾官方网站:购买化妆品和护肤品
2017/04/05 全球购物
几个Linux面试题笔试题
2016/08/01 面试题
公司年会晚宴演讲稿
2014/01/06 职场文书
总账会计岗位职责
2014/03/13 职场文书
大学生见习报告范文
2014/11/03 职场文书
党的群众路线教育实践活动心得体会(企业)
2014/11/03 职场文书
办公楼租房协议书范本
2014/11/25 职场文书
企业安全生产检查制度
2015/08/06 职场文书
Python实现socket库网络通信套接字
2021/06/04 Python