user_list=[ {'name':"alex","pwd":'123'}, {'name':'tang','pwd':'123'}, {'name':'sb','pwd':'123'} ] current_dict={'username':None,'login':False} def auth(auth_type): def auth_func(func): def wrapper(*args,**kwargs): print("认证类型", auth_type) if auth_type == 'filedb': if current_dict['username'] and current_dict['login']:#判断用户的登陆状态 res = func(*args, **kwargs) return res username=input("用户名").strip()#strip()函数去除字符串两边的空格 password=input('密码').strip() for item in user_list: if username==item['name'] and password==item['pwd']: current_dict['username']=username#改变用户的登陆状态,改变后就不需要每次都进行输入 current_dict['login']=True res=func(*args,**kwargs) return res else: print("用户名或密码错误") elif auth_type=="ldap": print("hahhaha") return wrapper return auth_func @auth(auth_type="filedb")#auth_func=auth(auth_type="filedb")--->@auth_func 附加了一个auth_type---->index=auth_func(index) def index(): print("欢迎来到京东") @auth(auth_type='') def home(name): print("我的个人中心%s"%(name)) @auth(auth_type='filedb') def shop_car(name): print("%s我的购物车"%(name)) print("after---->",current_dict) index() print('after---->',current_dict) home("alex") shop_car("产品经理") # dict_user={"username":None,"login":False} # # def auth_func(func): # def wrapper(*args,**kwargs): # if dict_user['username'] and dict_user['login']: # res = func(*args, **kwargs) # return res # username=input("用户名").strip()#strip()函数去除字符串两边的空格 # password=input('密码').strip() # if username=="sb" and password=="123": # dict_user['username']=username # dict_user['login']=True # res=func(*args,**kwargs) # return res # else: # print("用户名或密码错误") # # return wrapper # # # # # # # @auth_func # def index(): # print("欢迎来到京东") # # @auth_func # def home(name): # print("我的个人中心%s"%(name)) # # @auth_func # def shop_car(name): # print("%s我的购物车"%(name)) # # index() # home("alex") # shop_car("产品经理") # # # a="123" # b='1' # v=b.join(a)##b join a # print(v)