Python3.5装饰器典型案例分析

 更新时间:2019年04月30日 10:00:24   作者:loveliuzz  
这篇文章主要介绍了Python3.5装饰器,结合实例形式分析了装饰器修饰有参数函数、装饰器修饰函数参数等情况相关使用技巧,需要的朋友可以参考下

本文实例讲述了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 pandas模块进行数据分析

    python pandas模块进行数据分析

    Python的Pandas模块是一个强大的数据处理工具,可以用来读取、处理和分析各种数据,本文主要介绍了python pandas模块进行数据分析,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • python的构建工具setup.py的方法使用示例

    python的构建工具setup.py的方法使用示例

    本篇文章主要介绍了python的构建工具setup.py的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Python检查ping终端的方法

    Python检查ping终端的方法

    今天小编就为大家分享一篇Python检查ping终端的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 安装不同版本的tensorflow与models方法实现

    安装不同版本的tensorflow与models方法实现

    这篇文章主要介绍了安装不同版本的tensorflow与models方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python开发工具PyCharm的下载与安装步骤图文教程

    Python开发工具PyCharm的下载与安装步骤图文教程

    这篇文章主要为大家介绍了Python开发工具PyCharm的下载与安装步骤图文教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Python中的线程操作模块(oncurrent)

    Python中的线程操作模块(oncurrent)

    这篇文章介绍了Python中的线程操作模块(oncurrent),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • Python内置的字符串处理函数整理

    Python内置的字符串处理函数整理

    Python内置的字符串处理函数整理,收集常用的Python 内置的各种字符串处理 函数的使用方法
    2013-01-01
  • Python时间差中seconds和total_seconds的区别详解

    Python时间差中seconds和total_seconds的区别详解

    今天小编就为大家分享一篇Python时间差中seconds和total_seconds的区别详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Python实现自定义包的实例详解

    Python实现自定义包的实例详解

    这篇文章主要介绍了实现自定义包的方法,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • 分享8 个常用pandas的 index设置

    分享8 个常用pandas的 index设置

    这篇文章主要介绍了分享8 个常用pandas的 index设置,pandas 中的 index 是行索引或行标签。行标签可以说是 pandas 的灵魂一签,支撑了 pandas 很多强大的业务功能,比如多个数据框的 join, merge 操作,自动对齐等,下面来看看文章得具体介绍吧
    2021-12-12

最新评论