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()

原文地址:https://www.cnblogs.com/zaizaiaipython/p/7819546.html