python学习之路 六 :装饰器
2018-08-06 16:09
381 查看
本节重点:
-
掌握装饰器相关知识
python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。
装饰器扩展登录功能
import json def auth_user(username, password): user_dict = json.load(open("file/user1.txt", "r", encoding="gbk")) if username in user_dict: if password == user_dict[username]: return True return False login_status = False def login(fun): def inner(*args, **kwargs): global login_status if not login_status: username = input("用户名:").strip() password = input("密码:").strip() if auth_user(username, password): login_status = True else: print("wrong username or password") if login_status: fun(*args, **kwargs) return inner @login def dalu(): print(" 欢迎来到大陆电影 ".center(30, "-")) @login def hongkong(): print(" 欢迎来到香港电影 ".center(30, "-")) @login def rihan(): print(" 欢迎来到日韩电影 ".center(30, "-")) @login def oumei(): print(" 欢迎来到欧美电影 ".center(30, "-")) action_dict = { 1: dalu, 2: hongkong, 3: rihan, 4: oumei } if __name__ == '__main__': while True: choice = input("""--- 选择功能 --- 1.大陆电影 2.香港电影 3.日韩电影 4.欧美电影 choice:""").strip() if choice.isdigit() and int(choice) in action_dict: action_dict[int(choice)]()
可选择登录方式的装饰器(带参数的装饰器)
import json def auth_user(auth_type, username, password): auth_data = json.load(open("file/user2.txt", "r", encoding="gbk")) user_list = auth_data[auth_type] if username in user_list: if password == user_list[username]: return True return False login_status = False def login(auth_type): def auth(fun): def inner(*args, **kwargs): global login_status nonlocal auth_type if not login_status: username = input("用户名:").strip() password = input("密码:").strip() if auth_user(auth_type, username=username, password=password): login_status = True else: print("wrong username or password") if login_status: fun(*args, **kwargs) return inner return auth # @login("qq") def dalu(): print(" 欢迎来到大陆电影 ".center(30, "-")) # @login("wechat") def hongkong(): print(" 欢迎来到香港电影 ".center(30, "-")) # @login("qq") def rihan(): print(" 欢迎来到日韩电影 ".center(30, "-")) # @login("qq") def oumei(): print(" 欢迎来到欧美电影 ".center(30, "-")) action_dict = { 1: dalu, 2: hongkong, 3: rihan, 4: oumei } if __name__ == '__main__': while True: choice = input("""--- 选择功能 --- 1.大陆电影 2.香港电影 3.日韩电影 4.欧美电影 choice:""").strip() if choice.isdigit() and int(choice) in action_dict: auth_type = None if not login_status: auth_type = input("登录方式").strip() auth = login(auth_type) inner = auth(action_dict[int(choice)]) inner()
相关文章推荐
- python学习之路-4 内置函数和装饰器
- python 学习之路(装饰器)
- Python学习之路6 - 装饰器
- Python学习之路-初学篇之初识装饰器
- Python学习之路,简单的注册登录小屁程序
- Python学习(三):迭代器、生成器、装饰器、递归、算法、正则
- Python学习(九)——匿名函数、装饰器、偏函数
- python学习之路 (一)
- Python学习之路--初始
- 王亟亟的Python学习之路(10)-函数对象的作用域,函数作为返回值,闭包
- Python小白学习之路(四)——第一次练习题
- Python学习之路---Python模块
- Python学习之路——Linux基础之用户创建
- python开发学习-day04(迭代器、生成器、装饰器、二分查找、正则)
- Python深入学习之装饰器
- python学习之路之案例0(实现登录功能,登录错误次数超过3次,自动退出登录)
- python小菜的学习之路 python优秀特性
- 王亟亟的Python学习之路(七)-date,continue,迭代对象,生成器
- FireFly开发之路(一)python学习一
- python 函数装饰器学习