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三元运算符实现方法
Dec 17 Python
Python实现子类调用父类的方法
Nov 10 Python
python实现朴素贝叶斯算法
Nov 19 Python
Python面向对象程序设计类的多态用法详解
Apr 12 Python
python学习--使用QQ邮箱发送邮件代码实例
Apr 16 Python
Python读取xlsx文件的实现方法
Jul 04 Python
Django框架之登录后自定义跳转页面的实现方法
Jul 18 Python
nginx黑名单和django限速,最简单的防恶意请求方法分享
Aug 09 Python
python查看数据类型的方法
Oct 12 Python
Python 使用threading+Queue实现线程池示例
Dec 21 Python
pycharm中使用request和Pytest进行接口测试的方法
Jul 31 Python
Python使用华为API为图像设置多个锚点标签
Apr 12 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
从零开始 教你如何搭建Discuz!4.1论坛
2006/07/07 PHP
PHP实现仿百度文库,豆丁在线文档效果(word,excel,ppt转flash)
2016/03/10 PHP
PHPCMS V9 添加二级导航的思路详解
2016/10/20 PHP
初学Javascript的一些总结
2008/11/03 Javascript
让div层随鼠标移动的实现代码 ie ff
2009/12/18 Javascript
16个最流行的JavaScript框架[推荐]
2011/05/29 Javascript
js的hasownproperty使用示例
2014/03/02 Javascript
JavaScript和CSS交互的方法汇总
2014/12/02 Javascript
Javascript实现计算个人所得税
2015/05/10 Javascript
js模糊查询实例分享
2016/12/26 Javascript
前端分页功能的实现以及原理(jQuery)
2017/01/22 Javascript
微信小程序开发图片拖拽实例详解
2017/05/05 Javascript
基于JS实现仿京东搜索栏随滑动透明度渐变效果
2017/07/10 Javascript
微信小程序实现页面跳转传递参数(实体,对象)
2019/08/12 Javascript
vue组件入门知识全梳理
2020/09/21 Javascript
解决removeEventListener 无法清除监听的问题
2020/10/30 Javascript
python实现逆波兰计算表达式实例详解
2015/05/06 Python
分享一下Python 开发者节省时间的10个方法
2015/10/02 Python
Python用模块pytz来转换时区
2016/08/19 Python
itchat和matplotlib的结合使用爬取微信信息的实例
2017/08/25 Python
python导入坐标点的具体操作
2019/05/10 Python
Pandas DataFrame中的tuple元素遍历的实现
2019/10/23 Python
Python视频编辑库MoviePy的使用
2020/04/01 Python
python中shell执行知识点
2020/05/06 Python
Java的类与C++的类有什么不同
2014/01/18 面试题
DELPHI中如何调用API,可举例说明
2014/01/16 面试题
综合素质的自我鉴定
2013/10/07 职场文书
司法工作人员群众路线对照检查材料思想汇报
2014/09/30 职场文书
2014党支部对照检查材料思想汇报
2014/10/05 职场文书
文体活动总结
2015/02/04 职场文书
学校少先队工作总结
2015/08/12 职场文书
人事任命书范本
2015/09/21 职场文书
小学体育跳绳课教学反思
2016/02/16 职场文书
解决Goland 同一个package中函数互相调用的问题
2021/05/06 Golang
springcloud之Feign超时问题的解决
2021/06/24 Java/Android
MySQL中B树索引和B+树索引的区别详解
2022/03/03 MySQL