zoukankan      html  css  js  c++  java
  • 装饰器3(装饰函数带参数)

    基础的装饰器:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    username,password = "sunwei","123"
    
    def auth(func):
        def wrapper():
            user = input("please input your name:").strip()
            passwd = input("please input your password:").strip()
            if user == username and passwd == password:
                print("33[32;40m验证通过33[0m")
                func()
            else:
                print("33[31;40m验证失败33[0m")
        return wrapper
    
    @auth
    def test1():
        print("welcome to test1...")
    @auth
    def test2():
        print("welcome to test2...")
    test1()
    test2()

    升级一下,装饰器函数带参数....

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import time
    username,password = "sunwei","123"
    
    
    def auth(auth_type):
        def out_wrapper(func):
            def wrapper(*args,**kwargs):
                if auth_type == "local": 
                    user = input("please input your name:").strip()
                    passwd = input("please input your password:").strip()
                    if user == username and passwd == password:
                        print("33[32;40m验证通过33[0m")
                        func(*args,**kwargs)
                    else:
                        print("33[31;40m验证失败33[0m")
                else:
                    time.sleep(3)
                    exit("我不知道什么是ldap...")
            return wrapper
        return out_wrapper
    
    @auth(auth_type = "local") #test1 = auth(test1)
    def test1():
        print("welcome to test1...")
    @auth(auth_type = "ldap")
    def test2():
        print("welcome to test2...")
    test1()
    test2()
    在你说话之前,先听;在你回应之前,先想;在你消费之前,先挣;在你退出之前,先试
  • 相关阅读:
    053-146
    053-659
    053-658
    053-657
    053-656
    053-654
    053-655
    053-652
    053-651
    053-650
  • 原文地址:https://www.cnblogs.com/sunweigogogo/p/7617595.html
Copyright © 2011-2022 走看看