python菜鸟学习: 12. 装饰器的中级用法
2022/8/25 1:23:00
本文主要是介绍python菜鸟学习: 12. 装饰器的中级用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# -*- coding: utf-8 -*- username = "liyuzhoupan" password = "123" def author(wrappreType): print("wrappreType:", wrappreType) def outterwrapper(func): def wrapper(*args, **kwargs): if wrappreType == "login1": username1 = input("Username:").strip() password1 = input("Password:").strip() if username == username1 and password == password1: print("\033[32;1m Login success!\033[0m") return func(*args, **kwargs) else: exit("\033[31;1m login fail \033[0m") elif wrappreType == "login2": print("login2 login") return func(*args, **kwargs) else: print("xia J8 che") return wrapper return outterwrapper def hello_index(): print("welcome to index") @author(wrappreType="login1") # 调用装饰器,根据条件wrappreType进行调用 def hello_home(): print("this is hello_home") return "from home" @author(wrappreType="login2") def hello_bbs(): print("this is hello_index") return "from bbs" hello_index() print(hello_home()) print(hello_bbs())
这篇关于python菜鸟学习: 12. 装饰器的中级用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享