装饰器2


import time

user,passwd = 'lian','abc123'
def auth(func):
def wrapper(*args,**kwargs):
username = input("Username:").strip()
password = input("Password:").strip()
if user == username and passwd == password:
print('\033[32;1mUser has passed authentication\033[0m')
func(*args,**kwargs)
else:
exit("\033[32;1mInvalid username or password\033[0m")
return wrapper
def index():
print('welcome to index page')
@auth
def home():
print('welcome to home page')
@auth
def bbs():
print("welcome to bbs page")

index()
home()
bbs()

猜你喜欢

转载自www.cnblogs.com/rongye/p/9921772.html