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中判断文件编码的chardet(实例讲解)
Dec 21 Python
Python+matplotlib实现华丽的文本框演示代码
Jan 22 Python
Python 实现选择排序的算法步骤
Apr 22 Python
使用TensorFlow实现SVM
Sep 06 Python
Pandas读写CSV文件的方法示例
Mar 27 Python
python实现AES和RSA加解密的方法
Mar 28 Python
Django时区详解
Jul 24 Python
配置python的编程环境之Anaconda + VSCode的教程
Mar 29 Python
Python判断三段线能否构成三角形的代码
Apr 12 Python
Python监听键盘和鼠标事件的示例代码
Nov 18 Python
python中str内置函数用法总结
Dec 27 Python
DjangoRestFramework 使用 simpleJWT 登陆认证完整记录
Jun 22 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实现的获取URL信息的类
2007/01/02 PHP
php学习笔记(三)操作符与控制结构
2011/08/06 PHP
浅析PHP原理之变量分离/引用(Variables Separation)
2013/08/09 PHP
是 WordPress 让 PHP 更流行了 而不是框架
2016/02/03 PHP
Zend Studio使用技巧两则
2016/04/01 PHP
js 剪切板的用法(clipboardData.setData)与js match函数介绍
2013/11/19 Javascript
jQuery学习笔记之 Ajax操作篇(三) - 过程处理
2014/06/23 Javascript
分享一款基于jQuery的视频播放插件
2014/10/09 Javascript
javascript简单实现图片预加载
2014/12/03 Javascript
jQuery中remove()方法用法实例
2014/12/25 Javascript
JavaScript中实现单体模式分享
2015/01/29 Javascript
关于JavaScript限制字数的输入框的那些事
2016/08/14 Javascript
js友好的时间返回函数
2016/08/24 Javascript
vue.js  父向子组件传参的实例代码
2017/10/29 Javascript
vue+Element实现搜索关键字高亮功能
2019/05/28 Javascript
Python2.7编程中SQLite3基本操作方法示例
2017/08/09 Python
python+opencv轮廓检测代码解析
2018/01/05 Python
python版飞机大战代码分享
2018/11/20 Python
python使用knn实现特征向量分类
2018/12/26 Python
python爬虫获取小区经纬度以及结构化地址
2018/12/30 Python
Python设计模式之观察者模式原理与用法详解
2019/01/16 Python
python中aioysql(异步操作MySQL)的方法
2019/04/11 Python
网易2016研发工程师编程题 奖学金(python)
2019/06/19 Python
Pandas之ReIndex重新索引的实现
2019/06/25 Python
python 使用plt画图,去除图片四周的白边方法
2019/07/09 Python
opencv3/python 鼠标响应操作详解
2019/12/11 Python
在主流系统之上安装Pygame的方法
2020/05/20 Python
香港士多网上超级市场:Ztore
2021/01/09 全球购物
汽车检测与维修应届毕业生求职信
2013/10/19 职场文书
专科毕业生学习生活的自我评价
2013/10/26 职场文书
党委书记岗位职责
2013/11/24 职场文书
组织关系转移介绍信
2014/01/16 职场文书
高中数学教学反思
2014/01/30 职场文书
应届生求职自荐信范文
2015/03/04 职场文书
情况说明书格式及范文
2019/06/24 职场文书
Python基础之元组与文件知识总结
2021/05/19 Python