博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
装饰器的基本使用:用户登录
阅读量:4636 次
发布时间:2019-06-09

本文共 1027 字,大约阅读时间需要 3 分钟。

users_dic = [{
'name':'zsz','passwd':'123'}, {
'name':'alex','passwd':'123'}, {
'name':'lhf','passwd':'123'}]current_dic = {
"username":None,"Login":False}def auth_func(func): def wrapper(*args,**kwargs): if current_dic["username"] and current_dic["Login"]: res = func(*args,**kwargs) return res username = input("用户名:").strip() passwd = input("密码:").strip() for user_dic in users_dic: if username == user_dic['name'] and passwd == user_dic['passwd']: current_dic["username"] = username current_dic["Login"] = True res = func(*args, **kwargs) return res else: print("用户名或者密码错误") return wrapper@auth_func #home = auth_func(home)def home(): print("欢迎回家")@auth_func #home = auth_func(home)def shopping(): print("购物车里有:牛奶,面包,大米")home()shopping()

输出结果:

用户名:zsz

密码:123
欢迎回家
购物车里有:牛奶,面包,大米

转载于:https://www.cnblogs.com/zhangsenzhen/p/9392120.html

你可能感兴趣的文章
第五次作业
查看>>
织梦教程
查看>>
杭电多校 Harvest of Apples 莫队
查看>>
java 第11次作业:你能看懂就说明你理解了——this关键字
查看>>
C/C++心得-结构体
查看>>
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>
数值计算算法-多项式插值算法的实现与分析
查看>>
day8-异常处理与网络编程
查看>>
Python基础-time and datetime
查看>>
Linux epoll 笔记(高并发事件处理机制)
查看>>
shell脚本练习01
查看>>
WPF图标拾取器
查看>>
通过取父级for循环的i来理解闭包,iife,匿名函数
查看>>
HDU 3374 String Problem
查看>>
数据集
查看>>
打印python包含汉字报SyntaxError: Non-ASCII character '\xe4' in file
查看>>
[Leetcode] unique paths ii 独特路径
查看>>
HDU 1217 Arbitrage (Floyd + SPFA判环)
查看>>
IntelliJ idea学习资源
查看>>