码迷,mamicode.com
首页 > 其他好文 > 详细

修饰器-2

时间:2018-08-12 21:35:17      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:rgs   from   wro   print   home   NPU   UNC   wrapper   **kwargs   

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

标签:rgs   from   wro   print   home   NPU   UNC   wrapper   **kwargs   

原文地址:https://www.cnblogs.com/gaoxu366/p/9464079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!