python菜鸟学习: 12. 装饰器的中级用法
# -*- coding: utf-8 -*-
username = "liyuzhoupan"
password = "123"
def author(wrappreType):
print("wrappreType:", wrappreType)
def outterwrapper(func):
def wrapper(*args, **kwargs):
if wrappreType == "login1":
username1 = input("Username:").strip()
password1 = input("Password:").strip()
if username == username1 and password == password1:
print("\033[32;1m Login success!\033[0m")
return func(*args, **kwargs)
else:
exit("\033[31;1m login fail \033[0m")
elif wrappreType == "login2":
print("login2 login")
return func(*args, **kwargs)
else:
print("xia J8 che")
return wrapper
return outterwrapper
def hello_index():
print("welcome to index")
@author(wrappreType="login1") # 调用装饰器,根据条件wrappreType进行调用
def hello_home():
print("this is hello_home")
return "from home"
@author(wrappreType="login2")
def hello_bbs():
print("this is hello_index")
return "from bbs"
hello_index()
print(hello_home())
print(hello_bbs())
本文来自博客园,作者:鲤鱼洲畔,转载请注明原文链接:https://www.cnblogs.com/liyuzhoupan/p/16620095.html