不带参数的装饰器
# 不带参数的装饰器
login_status = False
def login(fun):
def inner(*args,**kwargs):
_name = "cmz"
_passwd = "cmz"
global login_status
if not login_status:
name = input("name>").strip()
pwd = input("pwd>").strip()
if name == _name and pwd == _passwd:
print("welcome")
login_status = True
if login_status:
fun(*args,**kwargs)
return inner
@login
def fun1():
print("f1")
@login
def fun2():
print("f2")
@login
def fun3():
print("f3")
fun1()
# fun2()
fun3()
带参数的装饰器
log_status = False
def login(auth_type):
def auth(fun):
def inner(*args,**kwargs):
if auth_type == "qq":
_name = "cmz"
_pwd = "cmz"
global log_status
if not log_status:
name = input("name> ").strip()
passwd = input("passwd > ").strip()
if name == _name and passwd == _pwd:
print("welcome..")
log_status = True
if log_status:
fun(*args,**kwargs)
else:
print("只支持qq")
return inner
return auth
@login("qq")
def fun1():
print("f1")
@login("ac")
def fun2():
print("f2")
fun1()
fun2()