首页 > 编程语言 > 详细

Python3中装饰器的使用

时间:2017-03-05 23:31:49      阅读:233      评论:0      收藏:0      [点我收藏+]

较为复杂的装饰器使用:

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("\033[32;1muser pass ~~~~~~\033[0m")
                    func(*args,**kwargs)
                else:
                    exit("\033[31;1minvalid user ~~~~~~\033[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

Python3中装饰器的使用

原文:http://www.cnblogs.com/hjc4025/p/6507110.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!