码迷,mamicode.com
首页 > 编程语言 > 详细

python装饰器

时间:2018-04-30 10:21:28      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:lse   welcome   装饰器   you   wrong   rom   添加   str   type   

# coding=utf-8
# 高阶函数 + 嵌套函数=》装饰器
#这个是一个为home页面和bbs论坛页面添加登录认证的一个装饰器 user = "stanlong" passwd = "abc123" def auth(auth_type): def outer_wrapper(func): def wrapper(*args, **kwargs): print("wrapper func args:",*args, **kwargs) if auth_type == "local": username = input("input your username:").strip() password = input("input your password:").strip() if user == username and passwd == password: res = func(*args, **kwargs) print("--------------after login----------------") return res else: exit("wrong username or password") elif auth_type == "ldap": print("ldap认证方式") return wrapper return outer_wrapper def index(): print("welcome to index") @auth(auth_type = "local") def home(): print("wlcome to home") return "from home" @auth(auth_type = "ldap") def bbs(): print("welcome to bbs") index() home() bbs()

  

python装饰器

标签:lse   welcome   装饰器   you   wrong   rom   添加   str   type   

原文地址:https://www.cnblogs.com/StanLong/p/8972604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!