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之数据序列化(json、pickle、shelve)
Mar 30 Python
Python语言描述KNN算法与Kd树
Dec 13 Python
简单的python协同过滤程序实例代码
Jan 31 Python
python 筛选数据集中列中value长度大于20的数据集方法
Jun 14 Python
numpy向空的二维数组中添加元素的方法
Nov 01 Python
pyttsx3实现中文文字转语音的方法
Dec 24 Python
flask框架单元测试原理与用法实例分析
Jul 23 Python
django创建最简单HTML页面跳转方法
Aug 16 Python
python实现视频读取和转化图片
Dec 10 Python
利用python3 的pygame模块实现塔防游戏
Dec 30 Python
python生成大写32位uuid代码
Mar 03 Python
Python实现Appium端口检测与释放的实现
Dec 31 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
BBS(php &amp; mysql)完整版(八)
2006/10/09 PHP
PHP 反向排序和随机排序代码
2010/06/30 PHP
ThinkPHP页面跳转success与error方法概述
2014/06/25 PHP
ThinkPHP实现带验证码的文件上传功能实例
2014/11/01 PHP
php数组操作之键名比较与差集、交集赋值的方法
2014/11/10 PHP
php 多文件上传的实现实例
2016/10/23 PHP
PHP二维数组实现去除重复项的方法【保留各个键值】
2017/12/21 PHP
另类调用flash无须激活的方法
2006/12/27 Javascript
JS实现鼠标经过好友列表中的好友头像时显示资料卡的效果
2014/07/02 Javascript
JavaScript基础篇(3)之Object、Function等引用类型
2015/11/30 Javascript
微信小程序 122100版本更新问题解决方案
2016/12/22 Javascript
JavaScript异步上传图片文件的实例代码
2017/07/04 Javascript
VUE + UEditor 单图片跨域上传功能的实现方法
2018/02/08 Javascript
Angular2 自定义表单验证器的实现方法
2018/12/14 Javascript
Electron 如何调用本地模块的方法
2019/02/01 Javascript
微信小程序云开发详细教程
2019/05/16 Javascript
Python中的XML库4Suite Server的介绍
2015/04/14 Python
python中获得当前目录和上级目录的实现方法
2017/10/12 Python
Centos 升级到python3后pip 无法使用的解决方法
2018/06/12 Python
pytorch .detach() .detach_() 和 .data用于切断反向传播的实现
2019/12/27 Python
浅谈spring boot 集成 log4j 解决与logback冲突的问题
2020/02/20 Python
解决pyecharts运行后产生的html文件用浏览器打开空白
2020/03/11 Python
Opencv python 图片生成视频的方法示例
2020/11/18 Python
Puritan’s Pride(普丽普莱)官方网站:美国最大最全的保健品公司之一
2016/10/23 全球购物
南非最大的花卉和送礼服务:NetFlorist
2017/09/13 全球购物
.NET笔试题(20个问题)
2016/02/02 面试题
创业者是否需要商业计划书?
2014/02/07 职场文书
讲文明树新风演讲稿
2014/05/12 职场文书
班子四风对照检查材料
2014/08/21 职场文书
小学庆六一活动总结
2014/08/28 职场文书
80后婚前协议书范本
2014/10/24 职场文书
小学家长意见怎么写
2015/06/03 职场文书
开学随笔
2015/08/15 职场文书
2019送给家人们的中秋节祝福语
2019/08/15 职场文书
500字作文之难忘的同学
2019/12/20 职场文书
SQL CASE 表达式的具体使用
2022/03/21 SQL Server