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

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

装饰器的演变过程:

例.实现一个函数测试电脑的读取速度并花了多少时间

1 import time 2 def gettime(arg): 3     starttime=time.time() 4     arg() 5     endtime=time.time() 6     print(endtime-starttime) 7  8 def go(): 9     lastnume=010     for i in range(100000000):11         lastnume+=112         print(lastnume)13 gettime(go)

装饰器本质: 装饰器=函数接口+嵌套函数 

原则:1.不能修改被装饰函数的源码    2.不能修改被装饰函数的调用方式

作用:为其他函数添加附加功能

 

1 # 装饰器 2 def gettime(arg): 3     "函数接口用于封装函数" 4     def warpper(*args,**kwargs): 5         "用于返回实现的功能" 6         starttime=time.time()   7         arg() 8         endtime=time.time() 9         print(endtime-starttime)10     return warpper11 12 @gettime    13 def go():14     lastnume=015     for i in range(100000000):16         lastnume+=117     print(lastnume)18 go()

登陆系统:

1 user,passwd = 'Temu','etc123123' 2 def auth(auth_type): 3     print("auth func:",auth_type) 4     def outer_wrapper(func): 5         def wrapper(*args, **kwargs): 6             print("wrapper func args:", *args, **kwargs) 7             if auth_type == "local": 8                 username = input("Username:").strip() 9                 password = input("Password:").strip()10                 if user == username and passwd == password:11                     print("\033[32;1mUser has passed authentication\033[0m")12                     res = func(*args, **kwargs)  # from home13                     print("---after authenticaion ")14                     return res15                 else:16                     exit("\033[31;1mInvalid username or password\033[0m")17             elif auth_type == "admin":18                 username = input("Username:").strip()19                 password = input("Password:").strip()20                 if user == username and passwd == password:21                     print("\033[33;1mUser has passed authentication\033[0m")22                     res2 = func(*args, **kwargs)  # from home23                     print("---after authenticaion ")24                     return res225                 else:26                     exit("\033[31;1mInvalid username or password\033[0m")27         return wrapper28     return outer_wrapper29 30 System=["Windows","Unix"]31 print(System[0].center(20," "),System[1].center(20," "))32 33 while True:34     User_coice=input("请选择你要登陆的系统:")35     if User_coice == "Windows":36         @auth(auth_type="admin")  #  windows37         def pcwin():38             print("*******Hello,Welcome to Windows page*******")39         pcwin()40         break41 42     elif User_coice == "Unix":43         @auth(auth_type="local")   # unix44         def home():45             print("Welcome to Home page")46             return "from home"47         print(home())48         break49     else:50         print("没有你要登陆的操作系统,请重新选择...")

 

转载于:https://www.cnblogs.com/gamaboy/p/7368329.html

你可能感兴趣的文章
Git 对象 和checkout 和stash的笔记
查看>>
团队项目总结2-服务器通信模型和顺序图
查看>>
hdu 1085 Holding Bin-Laden Captive!
查看>>
[周记]8.7~8.16
查看>>
递归定义
查看>>
kindeditor 代码高亮设置
查看>>
图的邻接表存储
查看>>
2018 leetcode
查看>>
PHP中获取当前页面的完整URL
查看>>
Chapter 4 Syntax Analysis
查看>>
vi/vim使用
查看>>
讨论Spring整合Mybatis时一级缓存失效得问题
查看>>
Maven私服配置Setting和Pom文件
查看>>
Xcode10 library not found for -lstdc++ 找不到问题
查看>>
Mysql 8.0.13如何重置密码
查看>>
发布功能完成
查看>>
excel 合并单元格
查看>>
How to Create Modifiers Using the API QP_MODIFIERS_PUB.PROCESS_MODIFIERS
查看>>
待飞笔记(第一天 )
查看>>
解惑好文:移动端H5页面高清多屏适配方案
查看>>