【Python知识点梳理】7.Python装饰器

7.Python装饰器


  Python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。

# 定义一个延时打印"hello world" 的函数
import time

def func():
    print("hello")
    time.sleep(2)
    print("world")


func()
# 原始侵入,篡改原函数,记录函数执行的总时间
import time

def func():
    startTime = time.time()

    print("hello")
    time.sleep(2)
    print("world")
    endTime = time.time()

    msecs = endTime - startTime
    print(msecs, "秒")


func()
# 避免直接侵入原函数修改,但是生效需要再次执行函数
# 定义一个函数deco(),它的参数是一个函数,然后给这个函数嵌入了计时功能。
import time

def deco(func):
    startTime = time.time()
    func()
    endTime = time.time()
    msecs = endTime - startTime
    print(msecs, "秒")


def func():
    print("hello")
    time.sleep(2)
    print("world")


if __name__ == '__main__':
    f = func
    deco(f)  # 只有把func()或者f()作为参数执行,新加入功能才会生效
    print("f.__name__ is", f.__name__)  # f的name就是func
# 既不需要侵入,也不需要函数重复执行
# 实现一个最简陋的装饰器,不使用任何语法糖和高级语法,看看装饰器最原始的面貌
# deco函数就是最原始的装饰器,它的参数是一个函数,然后返回值也是一个函数。
# 其中作为参数的这个函数func()就在返回函数wrapper()的内部执行。
# 然后在函数func()前面加上@deco,func()函数就相当于被注入了计时功能,
# 只要调用func(),它就已经变身为“新的功能更多”的函数了。

import time

def deco(func):
    def wrapper():
        startTime = time.time()
        func()
        endTime = time.time()
        msecs = endTime - startTime
        print(msecs, "秒")
    return wrapper


@deco
def func():
    print("hello")
    time.sleep(2)
    print("world")


if __name__ == '__main__':
    f = func  # 这里f被赋值为func,执行f()就是执行func()
    f()
# 带有参数的装饰器
import time

def deco(func):
    def wrapper(a, b):
        startTime = time.time()
        func(a, b)
        endTime = time.time()
        msecs = endTime - startTime
        print(msecs, "秒")
    return wrapper


@deco
def func(a, b):
    print("hello,here is a func for add :")
    time.sleep(2)
    print("result is %d" % (a+b))


if __name__ == '__main__':
    f = func
    f(3, 4)
    # func()
# 带有不定参数的装饰器
import time

def deco(func):
    def wrapper(*args, **kwargs):
        startTime = time.time()
        func(*args, **kwargs)
        endTime = time.time()
        msecs = endTime - startTime
        print(msecs, "秒")
    return wrapper


@deco
def func(a, b):
    print("hello,here is a func for add :")
    time.sleep(2)
    print("result is %d" % (a+b))


@deco
def func2(a, b, c):
    print("hello,here is a func for add :")
    time.sleep(2)
    print("result is %d" % (a+b+c))


if __name__ == '__main__':
    f = func
    func2(3, 4, 5)
    f(3, 4)
    # func()
#多个装饰器

import time

def deco01(func):
    def wrapper(*args, **kwargs):
        print("this is deco01")
        startTime = time.time()
        func(*args, **kwargs)
        endTime = time.time()
        msecs = endTime - startTime
        print(msecs, "秒")
        print("deco01 end here")
    return wrapper

def deco02(func):
    def wrapper(*args, **kwargs):
        print("this is deco02")
        func(*args, **kwargs)

        print("deco02 end here")
    return wrapper

@deco01
@deco02
def func(a,b):
    print("hello,here is a func for add :")
    time.sleep(2)
    print("result is %d" %(a+b))



if __name__ == '__main__':
    f = func
    f(3,4)
    #func()
# Python装饰器
import time

def timec(func):
    '''统计函数运行时间'''
    def warpper():
        start = time.time()
        func()
        end = time.time()
        used = end - start
        print(f"{func.__name__} used time {used}")
    return warpper

@timec
def step1():
    time.sleep(2)
    print("step1.....")

@timec
def step2():
    time.sleep(3)
    print("step2.....")

@timec
def step3():
    time.sleep(4)
    print("step3.....")
    
step1()
step2()
step3()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器视觉小学徒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值