返回顶部
扶摇直上九万里,展翅高飞岂可待。

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())
posted @ 2022-08-24 15:23  鲤鱼洲畔  阅读(18)  评论(0编辑  收藏  举报