Function closure adds authentication to functions

#Simulation authentication but there is a problem because the user name and password need to be repeatedly entered and solved in the following essay (simulation session) 

def
auth_func(func): def wrapped(*args,** kwargs): user_name = input( " Please enter your username " ).strip() passwd = input( " Please enter your password " ).strip() if user_name == " sb " and passwd == " 123 " : #Note that the username and password here are both strings, not numbers and must be quoted res = func(*args,** kwargs) return res #Pay attention to the position of return here else : print ( " Username or password is incorrect " ) return wrapped #Note that there are no parentheses here and it will not run wrapped @auth_func def index(): print ( " Welcome to JD.com " ) @auth_func def home(name): #Note that the function must have formal parameters print ( " Dear %s, welcome home " % name) @auth_func def shopping_car(name): print ( " %s has %s in the shopping cart, %s " %(name, " tt " , " doll " )) index() home( " Dragon Brother " ) #Pay attention to the parameters shopping_car( " Dragon Brother " )

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326171632&siteId=291194637