zoukankan      html  css  js  c++  java
  • python的有参装饰器

    def auth(db_type):
    def deco(func):
    def wrapper(*args, **kwargs):
    name = input('your name>>>:'.strip())
    pwd = input('your password>>>:'.strip())

    if db_type == 'file':
    print('基于文件的验证')
    # 从文件中取账号密码进行验证
    if name == 'engo' and pwd == '123':
    print('login successful')
    res = func(*args, **kwargs)
    return res
    else:
    print('user or password error')
    elif db_type == 'mysql':
    print('基于mysql的验证')
    elif db_type == 'ldap':
    print('基于ldap的验证')
    else:
    print('不支持改db_type')
    return wrapper

    return deco

    # deco = auth(db_type='file')


    @auth(db_type='file') # 碰到函数+(),先把@放一边,首先执行函数调用,然后结果是@函数返回值(@deco)
    # @deco会把index作为参数传入去调用函数d--eco(index)
    def index(x, y):
    print('index->>%s:%s' %(x, y))

     

     



  • 相关阅读:
    Ajax请求参数解释
    下拉菜单:‘点击外面关闭’的解决方案
    nc
    telnet
    arping
    traceroute
    ping
    ss
    netstat
    ip
  • 原文地址:https://www.cnblogs.com/kingchen/p/12976109.html
Copyright © 2011-2022 走看看