zoukankan      html  css  js  c++  java
  • Python入门:装饰器案例3

    import time

    user,passwd="LIly","abc123"

    def auth(auth_type):

      print("auth func:",auth_type)

      def outwrapper(func):

        def wrapper(*args,**kwargs):

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

          if auth_type=="local":

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

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

            if username==user and password==passwd :

              print("33[32:1mUser has passed authentication33[0m")

              res=func(*args,**kwargs) #from home

              return res

            else:

              exit("33[31;1mInvalid username or password33[0m")

          elif auth_type=="ldap":

            print("搞毛线ldap,不会。。。。")

            

           return wrapper

        return outwrapper

    def index():

      print("welcome to index page")

    @auth(auth_type="local")

    def home():

      print("welcome to home page")

      return "from home"

    @auth(auth_type="ladp") # home = wrapper()

    def bbs():

      print("welcome to bbs page")

    index()

    print(home())  #wrapper

    bbs()

  • 相关阅读:
    118/119. Pascal's Triangle/II
    160. Intersection of Two Linked Lists
    168. Excel Sheet Column Title
    167. Two Sum II
    172. Factorial Trailing Zeroes
    169. Majority Element
    189. Rotate Array
    202. Happy Number
    204. Count Primes
    MVC之Model元数据
  • 原文地址:https://www.cnblogs.com/luckerzhang/p/9305355.html
Copyright © 2011-2022 走看看