zoukankan      html  css  js  c++  java
  • python_63_装饰器6

    #decorator意思:1.装饰器  2.语法糖
    import time
    user,passwd='qi','123'
    def auth(func):
        def wrappper(*args, **kwargs):
            username = input('Username:').strip()
            password = input('password:').strip()
            if user == username and passwd == password:
                print('33[32;1mUser has passed authentication33[0m')
                res = func(*args, **kwargs)#from home
                print('---after authentiction')
                return res
                #func(*args, **kwargs)#如果将以上三步换成本步,则打印不出from home,可以换成 return func(*args, **kwargs)
            else:
                exit('33[31;1mInvalid username or password33[0m')
        return wrappper
    
    def index():#首页(不需要登录)
        print('Welcome to index page!')
    @auth
    def home(auth_type='local'):#主页(需要登录)
        print('Welcome to home page!')
        return 'from home'
    @auth
    def bbs(auth_type='ldap'):#论坛页(不需要登录 )
        print('Welcome to bbs page!')
    
    index()
    print(home())#调用home相当于调用wrapper()
    bbs()
    
  • 相关阅读:
    HttpWebRequest后台读取网页类
    MD5加密方法
    Base64封装类
    3DES封装类
    C#操作XML类
    XML_Qt_资料
    XML_CPP_资料
    h.264_javascript_资料
    ffmpeg_资料_01
    QWebEngineView_简单例子_01
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/8379914.html
Copyright © 2011-2022 走看看