python-验证功能的装饰器示例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-验证功能的装饰器示例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2007字,纯文字阅读大概需要3分钟。
内容图文
![python-验证功能的装饰器示例](/upload/InfoBanner/zyjiaocheng/776/1d53d66d08c54c618bafa3050eb7df4a.jpg)
user_list=[ {'name':"alex","pwd":'123'}, {'name':'tang','pwd':'123'}, {'name':'sb','pwd':'123'} ] 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-验证功能的装饰器示例全部内容,希望文章能够帮你解决python-验证功能的装饰器示例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。