首页 > 其他 > 详细

decorator高潮版

时间:2017-11-11 18:30:39      阅读:177      评论:0      收藏:0      [点我收藏+]

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

decorator高潮版

原文:http://www.cnblogs.com/zaizaiaipython/p/7819546.html

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