user_list=[
{'name':"alex","pwd":''},
{'name':'tang','pwd':''},
{'name':'sb','pwd':''}
]
current_dict={'username':None,'login':False}
def auth(auth_type):
def auth_func(func):
def wrapper(*args,**kwargs):
print("认证类型", auth_type) if auth_type == 'filedb': if current_dict['username'] and current_dict['login']:#判断用户的登陆状态
res = func(*args, **kwargs)
return res
username=input("用户名").strip()#strip()函数去除字符串两边的空格
password=input('密码').strip() for item in user_list:
if username==item['name'] and password==item['pwd']:
current_dict['username']=username#改变用户的登陆状态,改变后就不需要每次都进行输入
current_dict['login']=True
res=func(*args,**kwargs)
return res
else:
print("用户名或密码错误")
elif auth_type=="ldap":
print("hahhaha") return wrapper return auth_func @auth(auth_type="filedb")#auth_func=auth(auth_type="filedb")--->@auth_func 附加了一个auth_type---->index=auth_func(index)
def index():
print("欢迎来到京东") @auth(auth_type='')
def home(name):
print("我的个人中心%s"%(name)) @auth(auth_type='filedb')
def shop_car(name):
print("%s我的购物车"%(name)) print("after---->",current_dict)
index()
print('after---->',current_dict)
home("alex")
shop_car("产品经理") # dict_user={"username":None,"login":False}
#
# def auth_func(func):
# def wrapper(*args,**kwargs):
# if dict_user['username'] and dict_user['login']:
# res = func(*args, **kwargs)
# return res
# username=input("用户名").strip()#strip()函数去除字符串两边的空格
# password=input('密码').strip()
# if username=="sb" and password=="123":
# dict_user['username']=username
# dict_user['login']=True
# res=func(*args,**kwargs)
# return res
# else:
# print("用户名或密码错误")
#
# return wrapper
#
#
#
#
#
#
# @auth_func
# def index():
# print("欢迎来到京东")
#
# @auth_func
# def home(name):
# print("我的个人中心%s"%(name))
#
# @auth_func
# def shop_car(name):
# print("%s我的购物车"%(name))
#
# index()
# home("alex")
# shop_car("产品经理")
#
# # a="123"
# b='1'
# v=b.join(a)##b join a
# print(v)

python-验证功能的装饰器示例的更多相关文章

  1. Python中利用函数装饰器实现备忘功能

    Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下   " ...

  2. python 各种装饰器示例(python3)

    参考网址: Python中的各种装饰器详解_python_脚本之家http://www.jb51.net/article/63892.htm 一.函数式装饰器: 1.装饰器无参数,被装饰对象无参数 d ...

  3. python函数式编程之装饰器(一)

    1.开放封闭原则 简单来说,就是对扩展开放,对修改封闭 在面向对象的编程方式中,经常会定义各种函数. 一个函数的使用分为定义阶段和使用阶段,一个函数定义完成以后,可能会在很多位置被调用 这意味着如果函 ...

  4. Python之迭代器、装饰器、软件开发规范

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...

  5. python之闭包与装饰器

    python闭包与装饰器 闭包 在函数内部定义的函数包含对外部的作用域,而不是全局作用域名字的引用,这样的函数叫做闭包函数. 示例: #-------------------------------- ...

  6. python基础-函数之装饰器、迭代器与生成器

    1. 函数嵌套 1.1 函数嵌套调用 函数的嵌套调用:在调用一个函数的过程中,又调用了其他函数 def bar(): print("from in the bar.") def f ...

  7. python基础——8(装饰器)

    一.nonlocal关键字 def outer(): num = 0 def inner(): # 如果想在被嵌套的函数中修改外部函数变量(名字)的值 nonlocal num # 将 L 与 E(E ...

  8. Python函数编程——闭包和装饰器

    Python函数编程--闭包和装饰器 一.闭包 关于闭包,即函数定义和函数表达式位于另一个函数的函数体内(嵌套函数).而且,这些内部函数可以访问它们所在的外部函数中声明的所有局部变量.参数.当其中一个 ...

  9. Python函数篇:装饰器

    装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理. ...

随机推荐

  1. 在Python中,如何将一个字符串数组转换成整型数组

    https://blog.csdn.net/xiangchengguan/article/details/78987041 arr = ['] arr = list(map(int,arr)) pri ...

  2. UE4网络功能相关笔记

    RepNotity的作用 把变量设置成RepNotify除了像C#语言中的"属性"一样,提供一个改变变量时调用一个函数的机会以外,其真正重要的作用其实是应对网通同步延迟问题. 一定 ...

  3. es关闭不使用的index、真正删除文档

    因为只要索引处于open状态,就会占用内存+磁盘: 如果将索引close,只会占用磁盘 Curl -XPOST ‘hadoop01:9200/index/_close’ ------ 在es中删除文档 ...

  4. Failed to connect to github.com port 443: Timed out

    Git Clone下载仓库代码的时候,出现以下情况 Failed to connect to github.com port 443: Timed out 解决办法: 输入 git config -- ...

  5. Delphi XE2 之 FireMonkey 入门(28) - 数据绑定: TBindingsList: 表达式函数测试: SelectedText()、CheckedState()

    Delphi XE2 之 FireMonkey 入门(28) - 数据绑定: TBindingsList: 表达式函数测试: SelectedText().CheckedState() 示例构想: 用 ...

  6. ios-wx.chooseImage选择图片后,在ios中选中的图片未显示

    解决办法:微信引用1.2.0的版本 http://res.wx.qq.com/open/js/jweixin-1.2.0.js 我之前用的版本是:http://res.wx.qq.com/open/j ...

  7. 监控java的进程启动情况(bat)

    最近有个项目需要检测某个软件崩溃重启的间隔和重启时间,百度了一下,按照自己的需求做了相应的修改 @echo off rem 定义需监控程序的进程名和程序路径,可根据需要进行修改 set AppName ...

  8. python正则表达式整理

    正则表达式在处理字符串时很大的作用,爬虫中也经常用到,下面就将一些常用正则表达式做一整理记录,方便以后查看. ^d   表示匹配以d开头的字符串 .      表示匹配任意字符串 *     表示前面 ...

  9. 应用安全 - 安全设备 - WAF原理/检测/绕过

    原理 基于Cookie值 Citrix Netscaler(2013年使用广泛) “Citrix Netscaler”会在HTTP返回头部Cookie位置加入“ns_af”的值,可以以此判断为Citr ...

  10. Vue路由注意事项

    一.vue中路由的使用 1.定义组件 <template> <div class="hello"> <h1 @click="info&quo ...