• python基础----装饰器


    一、装饰器定义

    器即函数

    装饰即修饰,意指为其他函数添加新功能

    装饰器定义:本质就是函数,功能是为其他函数添加新功能

    二、装饰器需遵循的原则

    1.不修改被装饰函数的源代码(开放封闭原则)

    2.为被装饰函数添加新功能后,不修改被修饰函数的调用方式

    三、实现装饰器知识储备

    装饰器=高阶函数+函数嵌套+闭包

    四、无参装饰器

     1 #装饰器
     2 
     3 import time  #调用时间模块(系统自带的time模块)
     4 def timmer(func):
     5     def wrapper():
     6         #print(func)
     7         start_time=time.time()
     8         func()
     9         stop_time=time.time()
    10         print('run time is %s' %(stop_time - start_time))
    11     return wrapper
    12 
    13 @timmer    #index=timmer(index)
    14 def index():
    15     time.sleep(1)
    16     print('welcome to oldboy')
    17 index()    #   --------->wrapper()
    无参装饰器
     1 import time  #调用时间模块(系统自带的time模块)
     2 def timmer(func):
     3     def wrapper(*args,**kwargs):
     4         start_time=time.time()
     5         func(*args,**kwargs)
     6         stop_time=time.time()
     7         print('run time is %s' %(stop_time - start_time))
     8     return wrapper
     9 
    10 @timmer
    11 def home(name):
    12     time.sleep(2)
    13     print('welcome to %s home page'%name)
    14 
    15 @timmer
    16 def auth(name,password):
    17     print(name,password)
    18 
    19 @timmer
    20 def tell():
    21     print('------------------')
    22 
    23 home('dragon')
    24 auth('egon','123')
    25 tell()
    无参装饰器1
     1 import time  #调用时间模块(系统自带的time模块)
     2 def timmer(func):
     3     def wrapper(*args,**kwargs):
     4         start_time=time.time()
     5         res=func(*args,**kwargs)
     6         stop_time=time.time()
     7         print('run time is %s' %(stop_time - start_time))
     8         return res
     9     return wrapper
    10 
    11 @timmer
    12 def my_max(x,y):
    13     print('my_max function')
    14     res=x if x > y else y
    15     return res
    16 
    17 res=my_max(1,2)
    18 print('=====>',res)
    无参装饰器2

    五、有参装饰器

     1 def auth2(auth_type):
     2     def auth(func):
     3         #print(auth_type)
     4         def wrapper(*args,**kwargs):
     5             if auth_type == 'file':
     6                 name = input ('username: ')
     7                 password = input('password: ')
     8                 if name == 'egon' and password == '123':
     9                     print('auth successfull')
    10                     res=func(*args,**kwargs)
    11                     return res
    12                 else:
    13                     print('auth error')
    14             elif auth_type == 'sql':
    15                 print('还他妈不会玩')
    16         return wrapper
    17     return auth
    18 
    19 @auth2(auth_type='file')      #index=auth(index)             #这是通过本地文件方式读取
    20 def index():
    21     print('welcome to index page')
    22 index()
    23 
    24 
    25 @auth2(auth_type='sql')      #index=auth(index)                 #这是通过数据库方式读取
    26 def index():
    27     print('welcome to index page')
    28 index()
    有参装饰器
  • 相关阅读:
    前端开发:如何正确地跨端?
    开源微服务运行时 Dapr 发布 1.0 版本
    Flink 如何实时分析 Iceberg 数据湖的 CDC 数据
    为了让你在“口袋奇兵”聊遍全球,Serverless 做了什么?
    Serverless 如何在阿里巴巴实现规模化落地?
    altas(ajax)控件(十三):悬浮菜单HoverMenu
    MasterPage嵌套及MasterPage中的控件和变量的访问
    Linux下将CD音乐提取为mp3的方法和中文乱码问题的解决
    altas(ajax)控件(十四):为下拉控件添加快捷键查找功能的ListSearchExtender
    在atlas(ajax)中请不要使用Response.Write,请使用ClientScript.RegisterClientScriptBlock替换
  • 原文地址:https://www.cnblogs.com/wangyongsong/p/6689678.html
Copyright © 2020-2023  润新知