zoukankan      html  css  js  c++  java
  • Python3中装饰器的使用

    较为复杂的装饰器使用:

    user,passwd = 'hjc',111111
    
    def auth(type):
        print('auth type:',type)
        def outwrapper(func):
            def wrapper(*args,**kwargs):
                if type == 'lll':
                    username = input('username:').strip()
                    password = int(input('password:').strip())
                    if user == username and passwd == password:
                        print("33[32;1muser pass ~~~~~~33[0m")
                        func(*args,**kwargs)
                    else:
                        exit("33[31;1minvalid user ~~~~~~33[0m")
                elif type == 'kkk':
                    print('fuck off')
            return wrapper
        return outwrapper
    
    def index():
        print("welcome to index page")
    @auth('lll')
    def home():
        print("welcome to home page")
    @auth('kkk')
    def bbs():
        print("welcome to bbs page")
    index()
    home()
    bbs()
    

    输出结果如下:

    auth type: lll
    auth type: kkk
    welcome to index page
    username:hjc
    password:111111
    user pass ~~~~~~
    welcome to home page
    fuck off

    2.较为简单的装饰器:

    import time
    
    def timer(func):
        def inner(*args,**kwargs):
            start_time = time.time()
            func(*args,**kwargs)
            stop_time = time.time()
            print(stop_time-start_time)
        return inner
    
    
    @timer
    def wt():
        print('hello wt!!!')
    
    wt()
    

      

  • 相关阅读:
    10.用户管理
    9.更新系统时间
    8.标准输入输出重定向
    7.文件压缩与find命令
    6.Linux文件的详细属性
    5.Linux基础命令
    4.Linux目录结构
    3.磁盘光驱挂载
    2.xshell连接
    javascript中的location的用法
  • 原文地址:https://www.cnblogs.com/hjc4025/p/6507110.html
Copyright © 2011-2022 走看看