鱼C论坛

 找回密码
 立即注册
查看: 3277|回复: 0

[技术交流] 装饰器3decorator

[复制链接]
发表于 2020-9-22 10:35:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
装饰器3decorator:包含参数传递
  1. user, passwd = "Flagon", "123"


  2. def auth(auth_type):
  3.     print('auth func:', auth_type)
  4.     def outer_wrapper(func):
  5.         def wrapper(*args, **lwargs):
  6.             print('wrapper func args:', *args, **lwargs)
  7.             user_name = input('username:').strip()
  8.             password = input('password:').strip()
  9.             if auth_type == 'local':
  10.                 if user == user_name and passwd == password:
  11.                     print('\033[31:1mLogin success\033[0m', args)
  12.                     res = func(*args, **lwargs)  # 为了返回函数的值
  13.                     print('User has pass authentication!运行的函数名:', func.__name__)
  14.                     return res
  15.                 else:
  16.                     exit('\033[32:1mInvalid username or password\033[0m')
  17.             elif auth_type == 'ldap':
  18.                 res = func(*args, **lwargs)  # 为了返回函数的值
  19.                 print('User has pass authentication!运行的函数名:', func.__name__)
  20.                 print('搞毛线ldap,会不')
  21.                 return res
  22.         return wrapper
  23.     return outer_wrapper


  24. def index():
  25.     print('Welcome to be here!')


  26. @auth(auth_type='local')
  27. def home():
  28.     print('Welcom to the homepage!')
  29.     return "From home"


  30. @auth(auth_type='ldap')
  31. def bbs():
  32.     print('Welcom to the bbs!')
  33.     return 'From bbs'


  34. index()
  35. home()
  36. bbs()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-27 13:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表