# import time
#
# def timmer(func): #func=最原始的index的内存地址
# def wrapper(*args,**kwargs):
# start=time.time()
# res=func(*args,**kwargs)
# stop=time.time()
# print(stop - start)
# return res
# return wrapper
#
# @timmer #index=timmer(index) #index=wrapper的内存地址
# def index():
# print('index function')
# time.sleep(1)
#
# index(1,2,3,) #wrapper(1,2,3) # 叠加多个装饰器
#1. 加载顺序: 自下而上
#2. 执行顺序 # import time
#
# def deco1(func1): #func1=wrapper2的内存地址
# # print('deco1运行')
# def wrapper1(*args,**kwargs):
# print('wrapper1')
# res=func1(*args,**kwargs)
# return res
# return wrapper1
#
# def deco2(func2): #func2=wrapper3的内存地址
# # print('deco2运行')
# def wrapper2(*args,**kwargs):
# print('wrapper2')
# res=func2(*args,**kwargs)
# return res
# return wrapper2
#
# def deco3(func3): #func3=最原始那个index的内存地址
# # print('deco3运行')
# def wrapper3(*args,**kwargs):
# print('wrapper3')
# res=func3(*args,**kwargs)
# return res
# return wrapper3
#
#
# # index=wrapper1
# @deco1 # index=deco1(wrapper2)
# @deco2 # wrapper2=deco2(wrapper3)
# @deco3 # wrapper3=deco3(最原始那个index的内存地址)
# def index():
# print('index function')
# time.sleep(1)
#
# index() #wrapper1() import time
current_user={'username':None} def timmer(func): #func=最原始的index的内存地址
def wrapper1(*args,**kwargs):
start=time.time()
res=func(*args,**kwargs) #================>wrapper2
stop=time.time()
print(stop - start)
return res
return wrapper1 def auth(func):
def wrapper2(*args,**kwargs):
if current_user['username']:
res = func(*args, **kwargs)
return res
name=input('username>>: ').strip()
pwd=input('password>>: ').strip()
if name == 'egon' and pwd == '123':
current_user['username']=name
res=func(*args,**kwargs)
return res
else:
print('账号密码错误...')
return wrapper2 @timmer
@auth
def index():
print('index function')
time.sleep(1) index()
import time
current_user={'username':None} def outter(engine='file'):
def auth(func):
def wrapper2(*args,**kwargs):
# if current_user['username']:
# res = func(*args, **kwargs)
# return res
name=input('username>>: ').strip()
pwd=input('password>>: ').strip()
if engine == 'file':
print('基于文件的认证')
if name == 'egon' and pwd == '123':
current_user['username']=name
res=func(*args,**kwargs)
return res
else:
print('账号密码错误...')
elif engine == 'mysql':
print('基于mysql的认证')
elif engine == 'ldap':
print('基于ldap的认证')
else:
print("不合法的认证源")
return wrapper2
return auth @outter(engine='file') # 账号密码是来自于文件 index=aaa.txt(index) #index=wrapper2
def index():
print('index function')
time.sleep(1) @outter(engine='mysql') # 账号密码是来自于mysql # home=bbb(home) #home=wrapper2
def home(name):
print('home function',name)
time.sleep(1) index() #wrapper2()
home('egon') #wrapper2('egon')
 

Python记录11:叠加多个装饰器+有参装饰器的更多相关文章

  1. 【python基础】第19回 多层,有参装饰器 递归 二分法

    本章内容概要 1. 多层装饰器 2. 有参装饰器 3. 递归函数 4. 算法(二分法) 本章内容详解 1. 多层装饰器 1.1 什么是多层装饰器 多层装饰器是从下往上依次执行,需要注意的是,被装饰的函 ...

  2. python基础语法8 叠加装饰器,有参装饰器,wraps补充,迭代器

    叠加装饰器: 叠加装饰器 - 每一个新的功能都应该写一个新的装饰器 - 否则会导致,代码冗余,结构不清晰,可扩展性差 在同一个被装饰对象中,添加多个装饰器,并执行. @装饰1 @装饰2 @装饰3 de ...

  3. python函数:装饰器、修正、语法糖、有参装饰器、global与nonlocal

    一.装饰器 二.装饰器修正1 三.装饰器修正2 四.装饰器的语法糖 五.有参.无参装饰器 六.global与nonlocal 一.装饰器 ''' 1 什么是装饰器 器=>工具 装饰=>指的 ...

  4. python函数之有参装饰器

    一.为什么要有有参装饰器? 来看之前的无参装饰器 # 无参装饰器 def outter(func): def wrapper(*args,**kwargs): start = time.time() ...

  5. python函数:叠加装饰器、迭代器、自定义迭代器、生成式

    一.叠加多个装饰器二.迭代器三.自定义迭代器四.xxx生成式 一.叠加多个装饰器 # 加载装饰器就是将原函数名偷梁换柱成了装饰器最内层那个wrapper函数 # 在加载完毕后,调用原函数其实就是在调用 ...

  6. 十一. Python基础(11)—补充: 作用域 & 装饰器

    十一. Python基础(11)-补充: 作用域 & 装饰器 1 ● Python的作用域补遗 在C/C++等语言中, if语句等控制结构(control structure)会产生新的作用域 ...

  7. python学习笔记-day4笔记 常用内置函数与装饰器

    1.常用的python函数 abs             求绝对值 all               判断迭代器中所有的数据是否为真或者可迭代数据为空,返回真,否则返回假 any          ...

  8. python 有参装饰器与迭代器

    1.有参装饰器 模板: def auth(x): def deco(func): def timmer(*args,**kwargs ): res = func(*args,**kwargs ) re ...

  9. python 进阶篇 函数装饰器和类装饰器

    函数装饰器 简单装饰器 def my_decorator(func): def wrapper(): print('wrapper of decorator') func() return wrapp ...

随机推荐

  1. Sword libcurl回调函数相关知识

    libcurl响应回调函数说明   libcurl在默认情况下,回调里面会将数据分段的返回,不会一下子将发送端的数据全部塞到回调函数里面, 经过源码分析回调函数和curl_easy_perform是在 ...

  2. apache2.4 httpd.conf httpd-vhost.conf配置

    extra / httpd-vhost.conf <VirtualHost *:81> DocumentRoot "/data/sda1_data/" ServerNa ...

  3. ambari 安装HDP3.0.1后,启动服务的问题记录

    HDP的ambari集成安装工具真的是比ClouderaManager差上那么一点儿,不说安装的时候就麻烦,即使软件安装包已成功安装,也不意味着可以正常使用了,启动HDP集群过程中还会有不少的错误! ...

  4. springboot aop的execution 表达式详解

    Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execu ...

  5. How to write threats to validity?

    Paper reference Threats to construct validity are concerned with the relationship between theory and ...

  6. 这样使用 GPU 渲染 CSS 动画(转)

    大多数人知道现代网络浏览器使用GPU来渲染部分网页,特别是具有动画的部分. 例如,使用transform属性的CSS动画看起来比使用left和top属性的动画更平滑. 但是如果你问,“我如何从GPU获 ...

  7. crt sqlplus 中文乱码解决方案:

    1.确定数据库字符集 SQL> select userenv('language') from dual; USERENV('LANGUAGE') ----------------------- ...

  8. react使用BrowserRouter打包后,刷新页面出现404

    文档 https://gkedge.gitbooks.io/react-router-in-the-real/content/apache.html nginx nginx.conf server { ...

  9. react使用mobx

    mobx api 使用装饰器语法 mobx数据转化为js数据 安装 yarn add mobx mobx-react yarn add babel-preset-mobx --dev "pr ...

  10. oracle编码转换:AL32UTF8->ZHS16GBK

    --修改Oracle数据库字符集为utf-8: SQL>conn / as sysdba; SQL>shutdown immediate; SQL>startup mount; SQ ...