Python初步了解装饰器

Python初步了解装饰器

  • 装饰器的概念
  • 装饰器的简单使用
  • 装饰器的进阶
  • 装饰器的练习
  • 装饰器的固定模块
  • 装饰器的语法糖

装饰器的概念

装饰器它不是一个新的知识点,它是有之前我们学习的名称空间、函数嵌套、闭包函数等知识点汇总而来
器:工具
装饰:为其他事物添加功能
装饰器:不修该装饰的源代码的情况下,不改变源代码的调用的前提下,对装饰对象进行功能的增加。
核心思想
开放封闭原则:
开发:对扩展功能的开放,在源代码不改变的情况下,进行功能的添加
封闭:源代码是封闭的

import time#内置时间模块
def inside(group,s):
    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')
    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)#设置时间间隔
    print('全军出击')
inside('红色',30)

#tine.time()时间搓从1970年开始



方案1
#没有修改调用方式,倒是修改了源代码
import time#内置时间模块
def inside(group,s):
    stm_time=time.time()
    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')
    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')
    end_time=time.time()
    print(end_time-stm_time)
inside('红色',30)

装饰器的简单使用

方案二
代码冗余
import time
def inside(group,s):

    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')

    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')

   
stm_time=time.time()
inside('红色',30)
end_time=time.time()
print(end_time - stm_time)
方案三
调用方式被修改
import time
def inside(group,s):

    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')

    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')

def worap():
    stm_time=time.time()
    inside('红色',30)
    end_time=time.time()
    print(end_time - stm_time)
worap()
方案四
import time
def inside(group,s):

    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')

    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')


def worap(*args,**kwargs):
    stm_time=time.time()
    inside(*args,**kwargs)
    end_time=time.time()
    print(end_time - stm_time)
worap('蓝色',30)
方案五
import time
def inside(group,s):

    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')

    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')
def  ouct(func):
    # func=inside
    def worap(*args,**kwargs):
        stm_time=time.time()
        func(*args,**kwargs)
        end_time=time.time()
        print(end_time - stm_time)
    return worap
res=ouct(inside)
res('蓝色',10)




装饰器的进阶

方案六
import time
def inside(group,s):

    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')

    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')
def  ouct(func):
    # func=inside
    def worap(*args,**kwargs):
        stm_time=time.time()
        func(*args,**kwargs)
        end_time=time.time()
        print(end_time - stm_time)
    return worap
inside=ouct(inside)
inside('蓝色',10)

方案七
添加一个充电功能
import time
def inside(group,s):

    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')

    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')

def recharn(num):
    for i in range(num,101):
        time.sleep(0.05)
        print(f'\r当前电量{"‖"*i}{i}%',end='')
    print('电量已充满。')

def  ouct(func):
    # func=inside
    def worap(*args,**kwargs):
        stm_time=time.time()
        func(*args,**kwargs)
        end_time=time.time()
        print(end_time - stm_time)
    return worap
inside=ouct(inside)
inside('蓝色',10)
recharn=ouct(recharn)
recharn(20)
方案八
import time
def inside(group,s):

    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')

    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')

def recharn(num):
    for i in range(num,101):
        time.sleep(0.05)
        print(f'\r当前电量{"‖"*i}{i}%',end='')
    print('电量已充满。')

def  ouct(func):
    # func=inside
    def worap(*args,**kwargs):
        stm_time=time.time()
        res=func(*args,**kwargs)
        end_time=time.time()
        print(end_time - stm_time)
        return res
    return worap
recharn=ouct(recharn)
recharn(20)

装饰器的语法糖

方案九
import time
def  ouct(func):
    # func=inside
    def worap(*args,**kwargs):
        stm_time=time.time()
        res=func(*args,**kwargs)
        end_time=time.time()
        print(end_time - stm_time)
        return res
    return worap
@ouct
def inside(group,s):

    print('欢迎来到英雄联盟')
    print(f'你所在的是{group}方阵营')

    print(f'敌军还要{s}秒到达战场')
    time.sleep(s)
    print('全军出击')
@ouct
def recharn(num):
    for i in range(num,101):
        time.sleep(0.05)
        print(f'\r当前电量{"‖"*i}{i}%',end='')
    print('电量已充满。')
recharn(20)
inside('红色',10)

装饰器的固定模块

def oute(funs):
    def waropper(*arges,**kwargs):
        res=funs(*arges,**kwargs)
        return res
    return waropper


# 时间差模块
import time
def oute_time(funs):
    def waropper(*arges,**kwargs):
        cmd_time=time.time()
        res=funs(*arges,**kwargs)
        out_time=time.time()
        print(out_time-cmd_time)
        return res
    return waropper
@oute_time
def home():
    time.sleep(2)
    print('estdsfs')

热门相关:如果能少爱你一点   慕少,你老婆又重生了   慕少,你老婆又重生了   重生之嫡女祸妃   重生之女将星