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()
    在你说话之前,先听;在你回应之前,先想;在你消费之前,先挣;在你退出之前,先试
  • 相关阅读:
    截取字符串
    已解决 4G内存条,显示只有2.99G
    UIWindows
    视图控制对象的生命周期与内存过低警告
    Using Autorelease Pools
    About Windows and Views
    惠普武汉实习生面试20110320
    什么时候使用活动图!求指导!
    我看微软把[Show Desktop]移动的原因
    喷子们说百度的手机操作系统
  • 原文地址:https://www.cnblogs.com/sunweigogogo/p/7617595.html
Copyright © 2011-2022 走看看