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处理csv数据的方法
Mar 11 Python
对Python新手编程过程中如何规避一些常见问题的建议
Apr 01 Python
Python实现全角半角字符互转的方法
Nov 28 Python
Python金融数据可视化汇总
Nov 17 Python
详解tensorflow训练自己的数据集实现CNN图像分类
Feb 07 Python
Python内置模块hashlib、hmac与uuid用法分析
Feb 12 Python
Scrapy-Redis结合POST请求获取数据的方法示例
May 07 Python
Django生成PDF文档显示在网页上以及解决PDF中文显示乱码的问题
Jul 04 Python
Python 中如何实现参数化测试的方法示例
Dec 10 Python
python读取Kafka实例
Dec 23 Python
在python中使用pyspark读写Hive数据操作
Jun 06 Python
有关pycharm登录github时有的时候会报错connection reset的问题
Sep 15 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基础知识:类与对象(2) 自动加载对象
2006/12/13 PHP
php 表单数据的获取代码
2009/03/10 PHP
PHP 错误之引号中使用变量
2009/05/04 PHP
用php的ob_start来生成静态页面的方法分析
2011/03/09 PHP
文件上传之SWFUpload插件(代码)
2015/07/30 PHP
Zend Framework自定义Helper类相关注意事项总结
2016/03/14 PHP
基于CI(CodeIgniter)框架实现购物车功能的方法
2018/04/09 PHP
页面图片浮动左右滑动效果的简单实现案例
2014/02/10 Javascript
推荐 21 款优秀的高性能 Node.js 开发框架
2014/08/18 Javascript
JS操作COOKIE实现备忘记录的方法
2016/04/01 Javascript
分享12个非常实用的JavaScript小技巧
2016/05/11 Javascript
mac上node.js环境的安装测试
2017/07/03 Javascript
Vue.js实现分页查询功能
2020/11/15 Javascript
NodeJS搭建HTTP服务器的实现步骤
2018/10/12 NodeJs
JavaScript实现多张图片放大镜效果示例【不限定图片尺寸,rem单位】
2019/05/14 Javascript
JavaScript中如何对多维数组(矩阵)去重的实现
2019/12/04 Javascript
JS实现分页导航效果
2020/02/19 Javascript
在Python 3中实现类型检查器的简单方法
2015/07/03 Python
在CentOS上配置Nginx+Gunicorn+Python+Flask环境的教程
2016/06/07 Python
详解TensorFlow查看ckpt中变量的几种方法
2018/06/19 Python
selenium+python自动化测试之多窗口切换
2019/01/23 Python
python实现udp传输图片功能
2020/03/20 Python
Python json格式化打印实现过程解析
2020/07/21 Python
英国户外玩具儿童游乐设备网站:TP Toys(蹦床、攀爬框架、秋千、滑梯和游戏屋)
2018/04/09 全球购物
英国网上购买门:Direct Doors
2018/06/07 全球购物
TripAdvisor日本:全球领先的旅游网站
2019/02/14 全球购物
Structs界面控制层技术
2013/10/11 面试题
仓库主管的岗位职责
2013/12/04 职场文书
安全教育实施方案
2014/03/02 职场文书
入党积极分子自我鉴定范文
2014/03/25 职场文书
升学宴主持词
2014/04/02 职场文书
小学安全教育月活动总结
2014/07/07 职场文书
2015年学校财务工作总结
2015/05/19 职场文书
PHP判断是否是json字符串
2021/04/01 PHP
HTML5+CSS+JavaScript实现捉虫小游戏设计和实现
2021/10/16 HTML / CSS
win7配置本地ftp服务器的图文教程
2022/08/05 Servers