• 装饰器


    装饰器的演变过程:

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

     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=0
    10     for i in range(100000000):
    11         lastnume+=1
    12         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 warpper
    11 
    12 @gettime    
    13 def go():
    14     lastnume=0
    15     for i in range(100000000):
    16         lastnume+=1
    17     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("33[32;1mUser has passed authentication33[0m")
    12                     res = func(*args, **kwargs)  # from home
    13                     print("---after authenticaion ")
    14                     return res
    15                 else:
    16                     exit("33[31;1mInvalid username or password33[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("33[33;1mUser has passed authentication33[0m")
    22                     res2 = func(*args, **kwargs)  # from home
    23                     print("---after authenticaion ")
    24                     return res2
    25                 else:
    26                     exit("33[31;1mInvalid username or password33[0m")
    27         return wrapper
    28     return outer_wrapper
    29 
    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")  #  windows
    37         def pcwin():
    38             print("*******Hello,Welcome to Windows page*******")
    39         pcwin()
    40         break
    41 
    42     elif User_coice == "Unix":
    43         @auth(auth_type="local")   # unix
    44         def home():
    45             print("Welcome to Home page")
    46             return "from home"
    47         print(home())
    48         break
    49     else:
    50         print("没有你要登陆的操作系统,请重新选择...")
    即使明日天寒地冻路远马亡
  • 相关阅读:
    37 什么时候使用内部临时表
    2 Hadoop集群安装部署准备
    36 为什么临时表可以重名
    maven内置属性
    ProGuard 最全混淆规则说明
    stylus的用法
    vscode 插件 配置
    关于overflow:hidden
    vue-devtools/安装vue-devtools
    对Java中使用两个大括号进行初始化的理解
  • 原文地址:https://www.cnblogs.com/gamaboy/p/7368329.html
Copyright © 2020-2023  润新知