首页 > 编程语言 >Python初步了解装饰器

Python初步了解装饰器

时间:2023-09-08 20:33:04浏览次数:38  
标签:end Python inside 初步 time print group 装饰 def

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')

标签:end,Python,inside,初步,time,print,group,装饰,def
From: https://www.cnblogs.com/zhangfanshixiaobai/p/17688503.html

相关文章

  • Alembic:Python数据库迁移工具
    Alembic是一款轻量型的数据库迁移工具,它与SQLAlchemy一起共同为Python提供数据库管理与迁移支持。Alembic的应用Alembic使用SQLAlchemy作为数据库引擎,为关系型数据提供创建、管理、更改和调用的管理脚本,协助开发和运维人员在系统上线后对数据库进行在线管理。同任何P......
  • appium+python安装后代码测试
     fromappiumimportwebdriver#模拟器/真机已经被电脑识别到(adbdevices)#字典#1、设置终端参数项desired_caps={"platformName":"Android","platformVersion":"6.1.1","appPackage":"com.ss.android.article.n......
  • AI实战——跟着b站up主初步了解
    Python+人工智能通俗易懂版教学BV1ou411U7J4安装Python/anaconda/jupyter【注意:anacondanavigitor没有自动安装、jupyter学会改UI】了解python基本语法了解工具包matplotlib/numpy/pandas第一个用于作图,第二个用于数学运算,第三个用于数据筛选处理什么是机器学习?什么是监......
  • python3之os库和pathlib库
    #os标准库importos#当前Python运行所在的环境posix,nt,javaprint(os.name)#nt#os.getcwd()获取当前工作路径print(os.getcwd())dir="./files/foo/bar/bar2"file=os.path.join(dir,"c.txt")#os.path.join()可以将多个传入路径组合为一个路径print(file)......
  • python3类实例和错误处理
    类实例classCar(object):name="Car"def__init__(self,name):self.name=name#类方法通过@classmethod装饰器实现,只能访问类变量,不能访问实例变量;通过cls参数传递当前类对象,不需要实例化。@classmethoddefrun(cls,speed):......
  • python 格式输出
    格式化输出目录格式化输出1使用"%"1.1格式符1.2字符串输出(%s)1.3浮点数输出(%f)2使用format2.1位置匹配2.2格式转换2.3高阶用法python格式有两种方法:"%"和format1使用"%"1.1格式符格式符描述%s字符串(采用str()的显示)%r字符串(采用repr()的显示......
  • python写的文件比对脚本:
    上代码:#-*-coding:utf-8-*-importdifflib,webbrowserimportosimporttkinterastkfromtkinterimportfiledialog,messageboxdefreadfile(filename):try:withopen(filename,'r+',encoding='utf-8')asf:text=f.read().splitlines......
  • python内置定时任务
    目录python内置定时任务whileTrue+sleepTimeloopTimerschedpython内置定时任务whileTrue+sleepimportdatetimedeftask_run(*args,**kwargs):print(f"耗时操作....{datetime.datetime.now().strftime('%Y%m%d_%H:%M:%S')}")defcron_and_sleep():......
  • 在线问诊 Python、FastAPI、Neo4j — 创建节点
    目录前提条件创建节点Demo准备数据在线问诊Python、FastAPI、Neo4j—创建节点Neo4j节点的标签可以理解为Java中的实体。根据常规流程:首先有什么症状,做哪些对应的检查,根据检查诊断什么疾病,需要用什么药物治疗,服药期间要注意哪些饮食,需要做哪些运行在线问诊大概创建:症状......
  • Python中的异常处理机制
    finally语句是Python中异常处理机制的一部分,它总是会被执行,无论是否发生异常。finally语句通常用于释放资源或执行清理操作。下面是一个简单的例子:try:#代码段1passexceptExceptionType:#代码段2passelse:#代码段3passfinally:#代码段4......