zoukankan      html  css  js  c++  java
  • 装饰器3decorator

    装饰器3decorator:包含参数传递

     1 user, passwd = "Flagon", "123"
     2 
     3 
     4 def auth(auth_type):
     5     print('auth func:', auth_type)
     6     def outer_wrapper(func):
     7         def wrapper(*args, **lwargs):
     8             print('wrapper func args:', *args, **lwargs)
     9             user_name = input('username:').strip()
    10             password = input('password:').strip()
    11             if auth_type == 'local':
    12                 if user == user_name and passwd == password:
    13                     print('33[31:1mLogin success33[0m', args)
    14                     res = func(*args, **lwargs)  # 为了返回函数的值
    15                     print('User has pass authentication!运行的函数名:', func.__name__)
    16                     return res
    17                 else:
    18                     exit('33[32:1mInvalid username or password33[0m')
    19             elif auth_type == 'ldap':
    20                 res = func(*args, **lwargs)  # 为了返回函数的值
    21                 print('User has pass authentication!运行的函数名:', func.__name__)
    22                 print('搞毛线ldap,会不')
    23                 return res
    24         return wrapper
    25     return outer_wrapper
    26 
    27 
    28 def index():
    29     print('Welcome to be here!')
    30 
    31 
    32 @auth(auth_type='local')
    33 def home():
    34     print('Welcom to the homepage!')
    35     return "From home"
    36 
    37 
    38 @auth(auth_type='ldap')
    39 def bbs():
    40     print('Welcom to the bbs!')
    41     return 'From bbs'
    42 
    43 
    44 index()
    45 home()
    46 bbs()
    View Code
  • 相关阅读:
    软件测试分类与分级
    项目风险管理(Project Risk Management)
    软件测试基础
    【1】开关电源纹波的抑制
    EMC小知识
    【02】STM32:跑马灯配置
    【01】STM32:GPIO管脚模式设置
    【07】Java入门07:继承与抽象类
    【06】Java入门06:IO流-基础
    【05】Java入门05:Java集合
  • 原文地址:https://www.cnblogs.com/gzj137070928/p/13710607.html
Copyright © 2011-2022 走看看