zoukankan      html  css  js  c++  java
  • 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()
    有参装饰器
  • 相关阅读:
    短时间内点击 同一操作,进行缓存
    git项目如何查看代码提交数量
    Python计数器collections.Counter用法详解
    SQL Server如何生成随机数
    博客园美化 | part04-添加代码复制按钮
    博客园美化 | part03-添加目录
    博客园美化 | part01-自定义代码块主题样式
    博客园美化 | part02-添加文章markdown链接一键复制按钮
    hexo | leancloud相关问题: Code 403: Access denied by API domain white list,Please check your security domain.
    关于分类和标签管理问题
  • 原文地址:https://www.cnblogs.com/wangyongsong/p/6689678.html
Copyright © 2011-2022 走看看