zoukankan      html  css  js  c++  java
  • decorator高潮版

    import time

    user,passwd = 'zaizai','abc123'

    def auth(auth_type):

          print("auth func:",auth_type)

          def outer_wrapper(func):

                 def wrapper(*args,**kwargs)

                       print("wrapper func args:",*args,**kwargs)

                       if auth_type == "local":

                          username = input("Username:").strip()

                          password = input("Password:").strip()

                          if user == username and passwd == password:

                                 print("User has passed authtication")

                                 res = func(*args,**kwargs)

                                 return res

                           else:

                                  exit("Invalid username or password")

                       elif auth_type == "ldap"

                               print("搞毛线ldap")

                 return wrapper      

          return outer_wrapper

    def index():

          print("welcome to index page")

    @auth(auth_type"local") 

    def home():

          print("welcome to home page")

          return "from home"

    @auth(auth_type="ldap")

    def bbs():

          print("welcome to bbs page")

    index()

    print(home())

    bbs()

  • 相关阅读:
    python的类基础
    python导入模块
    python常用的内置函数
    python基础一数据类型之集合
    python函数-匿名函数
    python的函数(三)
    python的函数(二)
    python的函数(一)
    BZOJ4104:[Thu Summer Camp 2015]解密运算——题解
    BZOJ4033:[HAOI2015]树上染色——题解
  • 原文地址:https://www.cnblogs.com/zaizaiaipython/p/7819546.html
Copyright © 2011-2022 走看看