ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python装饰器的使用

2019-08-20 15:40:38  阅读:239  来源: 互联网

标签:do python time wrapper something 使用 装饰 decorator


前言:

在web开发过程中,经常会遇到数据查询慢的问题,对于查询一些时效性不是特别强的数据可以将数据缓存到redis中, 查询的时候直接到缓存中读取数据,这样就可以大大加快查询速度。
  但是在开发前,事先没有增加缓存这个功能,并且需要增加缓存功能的地方又有很多,为了在不改变原有代码的基础上添加新的功能,这就要用到python的装饰器。这里简要记录一下装饰器的使用!

说明:

弄清了装饰器函数的执行流程,以及参数如何传递等,装饰器的使用基本上就差不多了。这里通过一下几步来学习装饰器的使用:
  1. 装饰器的引入;
  2. 被装饰函数无参的装饰器;
  3. 被装饰函数带参的装饰器;
  4. 装饰器带参的装饰器。

装饰器的引入

# coding:utf-8

import time

def decorator(fun):
    """统计函数的运行时间"""
    start = time.time()
    fun()
    end = time.time()
    print end - start

def do_something():
    for i in range(5):
        time.sleep(1)
        
decorator(do_something)

代码解读:
  将do_something函数作为decorator函数的参数传入,在decorator函数中实现新的业务逻辑,并调用do_something函数, 这样就实现了在添加新功能的同时,不改变原有代码的逻辑,该写法不够简洁。

被装饰函数无参的装饰器


# coding:utf-8

import time

def decorator(fun):
    def wrapper():
        start = time.time()
        fun()
        end = time.time()
        print end - start

    return wrapper

@decorator
def do_something():
    for i in range(5):
        time.sleep(1)

do_something()
# 等价于:decorator(do_something)()

代码解读:
do_something()添加装饰器后,等价于do_something() = decorator(do_something)(),执行过程如下:

执行decorator方法,并将do_something方法作为参数传递;
返回内层函数wrapper的引用;
执行wrapper方法,等价于decorator(do_something)()

被装饰函数带参的装饰器


# coding:utf-8

import time

def decorator(fun):
    def wrapper(*args, **kwargs):
        start = time.time()
        fun(*args, **kwargs)
        end = time.time()
        print end - start

    return wrapper

@decorator
def do_something(times):
    for i in range(times):
        time.sleep(1)

do_something(5)
# 等价于:decorator(do_something)(5)

代码解读:
do_something(params)添加装饰器后,等价于do_something(params) = decorator(do_something)(params),执行过程如下:

  • 执行decorator方法,并将do_something方法作为参数传递;
  • 返回内层函数wrapper的引用;
  • 执行wrapper方法,等价于decorator(do_something)(params),可知,参数是通过内层函数wrapper传递进来的。

装饰器带参的装饰器


# coding:utf-8

import time

def decorator(name="带参装饰器"):
    def wrapper(fun):
        def inner(*args, **kwargs):
            start = time.time()
            fun(*args, **kwargs)
            end = time.time()
            print end - start
            print name
        return inner
    return wrapper

@decorator(name="带参装饰器")
def do_something(times):
    for i in range(times):
        time.sleep(1)

do_something(5)
# 等价于:decorator("带参装饰器")(do_something)(params)

如果你依然在编程的世界里迷茫,
不知道自己的未来规划,
对python感兴趣,
这里推荐一下我的学习交流圈:832 139 748,
里面都是学习python的,

代码解读:

do_something(params)添加装饰器后,等价于do_something(params) = decorator(“带参装饰器”)(do_something)(params),执行过程如下:

  • 执行decorator方法,并传递参数name,
  • 返回内层函数wrapper的引用,wrapper = decorator(“带参装饰器”)
  • 执行wrapper方法, 并传递do_something方法的引用,返回inner函数的引用,inner = decorator(“带参装饰器”)(do_something)
  • 执行inner方法,并传递参数params,实现inner方法的内部逻辑, 即:decorator(“带参装饰器”)(do_something)(params)

标签:do,python,time,wrapper,something,使用,装饰,decorator
来源: https://blog.csdn.net/XIAOYUE3035/article/details/99849441

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有