码迷,mamicode.com
首页 > 其他好文 > 详细

装饰器

时间:2018-06-15 15:58:46      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:bsp   ...   return   wrap   nbsp   use   index   turn   lob   

1

#!/usr/bin/env python3
# _*_ coding:utf-8 _*_

login_status=0

def login(arg):
    def wrapper(fun1):
        def inner(k1):
            global login_status
            if login_status==0:
                username = input(‘username: ‘)
                password = input(‘password: ‘)
                if arg==‘web‘:
                    if username==‘a‘ and password==‘a‘:
                        login_status=1
                    else:
                        print("login error")
                        return 0
                elif arg==‘weixin‘:
                    if username==‘b‘ and password==‘b‘:
                        login_status=1
                    else:
                        print(‘login error‘)
                        return 0
            elif login_status==1:
                pass
            print(‘in decorater...‘)
            fun1(k1)
        return inner
    return wrapper



@login(‘web‘)
def index(url):
    print(‘in index/%s‘%url)

@login(‘weixin‘)
def pay(url):
    print(‘in pay/%s‘%url)


while True:
    choice=input(‘select 123:\n‘)
    choice_dict={‘1‘:index,‘2‘:pay}
    choice_dict.get(choice)(‘abc‘)

 

装饰器

标签:bsp   ...   return   wrap   nbsp   use   index   turn   lob   

原文地址:https://www.cnblogs.com/infaaf/p/9186983.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!