1. 程式人生 > >修飾器-2

修飾器-2

return you rom input 執行 per ron swd username

import time
user,passwd = "gx","gx123"
def auth(func):
def wrapper(*args,**kwargs):
username = input("username:").strip()
password = input("password:").strip()
if user == username and passwd == password:
print("you has passed")
f = func(*args,**kwargs)#func執行後沒有返回值,沒有傳給誰
print(f)
else:
exit("you are wrong")
return wrapper

def index():
print("welcome to the index")
@auth
def home():
print("welcome to the home")
return "from home" #print(f),這時候return值傳給了f,所以from home就顯示出來了
@auth
def bbs():
print("welcome to the bbs")


index()
home() #調用home == 調用 wrapper
bbs()

修飾器-2