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 相关文章推荐
Windows系统下使用flup搭建Nginx和Python环境的方法
Dec 25 Python
Python实现的多线程http压力测试代码
Feb 08 Python
Python定义二叉树及4种遍历方法实例详解
Jul 05 Python
详解Python最长公共子串和最长公共子序列的实现
Jul 07 Python
Linux下Python安装完成后使用pip命令的详细教程
Nov 22 Python
python学生管理系统学习笔记
Mar 19 Python
Python 20行简单实现有道在线翻译的详解
May 15 Python
pandas.read_csv参数详解(小结)
Jun 21 Python
Django之路由层的实现
Sep 09 Python
python实现的读取网页并分词功能示例
Oct 29 Python
pytorch实现保证每次运行使用的随机数都相同
Feb 20 Python
python中取绝对值简单方法总结
Jul 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
phpMyAdmin出现无法载入 mcrypt 扩展,请检查PHP配置的解决方法
2012/03/26 PHP
PHP图像处理之imagecreate、imagedestroy函数介绍
2014/11/19 PHP
完美解决php 导出excle的.csv格式的数据时乱码问题
2017/02/18 PHP
Yii2压缩PHP中模板代码的输出问题
2018/08/28 PHP
ExtJS 2.0 实用简明教程之布局概述
2009/04/29 Javascript
ExtJS 工具栏 分页事件参数
2010/03/05 Javascript
javascript innerHTML使用分析
2010/12/03 Javascript
Jquery Ajax请求代码(2)
2011/01/07 Javascript
JS分割字符串并放入数组的函数
2011/07/04 Javascript
批量实现面向对象的实例代码
2013/07/01 Javascript
Jquery实现网页跳转或用命令打开指定网页的解决方法
2013/07/09 Javascript
原生js实现复制对象、扩展对象 类似jquery中的extend()方法
2014/08/30 Javascript
详解Javascript动态操作CSS
2014/12/08 Javascript
jQuery中:first-child选择器用法实例
2014/12/31 Javascript
javascript字符串替换函数如何一次性全部替换掉
2015/10/30 Javascript
Web安全测试之XSS实例讲解
2016/08/15 Javascript
微信小程序 开发经验整理
2017/02/15 Javascript
微信小程序 设置启动页面的两种方法
2017/03/09 Javascript
ES6中Proxy与Reflect实现重载(overload)的方法
2017/03/30 Javascript
jQuery实现动态生成表格并为行绑定单击变色动作的方法
2017/04/17 jQuery
怎么使用javascript深度拷贝一个数组
2019/06/06 Javascript
vue el-table实现自定义表头
2019/12/11 Javascript
jQuery实现电梯导航模块
2020/12/22 jQuery
Python实现的tab文件操作类分享
2014/11/20 Python
Linux环境下MySQL-python安装过程分享
2015/02/02 Python
简单介绍Python中的len()函数的使用
2015/04/07 Python
python实现图书管理系统
2018/03/12 Python
Python基于SMTP协议实现发送邮件功能详解
2018/08/14 Python
tensorflow使用神经网络实现mnist分类
2018/09/08 Python
如何用Python来搭建一个简单的推荐系统
2019/08/07 Python
python爬虫豆瓣网的模拟登录实现
2019/08/21 Python
简历中自我评价范文3则
2013/12/14 职场文书
仓库主管岗位职责
2014/03/02 职场文书
农村葬礼主持词
2014/03/31 职场文书
搞笑老公保证书
2015/02/26 职场文书
python中 .npy文件的读写操作实例
2022/04/14 Python