# 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. 关于Cocos2d-x-3.16的开发环境搭建

    一.需要安装的软件 1.VS2013或者VS2015 2.Cocos:cocos2d-x-3.16:http://www.cocos.com/download 3.Python:python-2.7. ...

  2. 为何GET只发一次TCP连接,POST发两次TCP连接

    GET和POST是HTTP请求的两种基本方法,要说他们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  3. [Golang] kafka集群搭建和golang版生产者和消费者

    一.kafka集群搭建 至于kafka是什么我都不多做介绍了,网上写的已经非常详尽了. 1. 下载zookeeper  https://zookeeper.apache.org/releases.ht ...

  4. cf 893 E

    有  次询问,第  次询问包含两个数  . 求满足下面两个要求的  数组的方案数. 1.  数组由  个整数构成 2.  A与B不同当且仅当至少存在一个数  满足  .答案对  取模 数据范围:  显 ...

  5. 剑指offer——python【第21题】栈的压入、弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...

  6. php(面向对象的基本介绍)

    面向对象思想介绍 OOP:Object Oriented Program面向对象编程. 面向对象三大特性 封装   继承   多态 类与对象 类:是用于描述“某一些具有共同特征”的物体的概念,是某一类 ...

  7. JAVA SE ------------------- 项目的菜单输入

    //写一个工具类,进行输入选项数值的获取public class InputUtil { static Scanner sc=new Scanner(System.in); public static ...

  8. inner_product

    版本1: template < class InputIterator1, class InputIterator2, class T> T inner_product(InputIter ...

  9. 个人小爱好:Operating System:three easy pieces---第6章第4节_担心并发问题?

    担心并发问题? 微妙,上下文切换大约6微妙.而,现在的系统有着级数级别的提升,在2-3GHz的处理起中消耗只有亚微妙级. 但应该注意到,不是所有的系统性能都跟着CPU性能的提升而提升,根据Ouster ...

  10. linux_vim_emmet插件的安装配置

    首先要去如下网址下载一个安装包(英文基础好的同学可以去github上搜他的开源,写的更加详细) https://www.vim.org/scripts/script.php?script_id=298 ...