Python基础-装饰器

装饰器与认证实践
本文介绍了Python装饰器的概念及其在用户认证中的应用。装饰器作为高阶函数的一种实现方式,可以为其他函数添加额外的功能而无需修改其源代码。文中通过具体示例展示了如何使用装饰器实现用户登录认证。

装饰器:
 *定义:本质就是函数,功能是为其他函数添加附加功能
 *原则:不修改被修饰函数的源代码/调用方式
 *知识储备:装饰器=高阶函数+函数嵌套+闭包
高阶函数:
 *定义:函数接收的参数是一个函数名/函数的返回值是一个函数名,满足其中任意条件被称为高阶函数
函数嵌套:
 *定义:在函数里面定义另外一个函数
  例: def father(name):
             print(name)
             def son(sonname):
                  print(sonname)
             print(locals())
    注意:打印的内容是局部变量的数值,并且以字典的形式返回。{"name":str,"son":son的函数内存地址}
闭包:
 融合在函数嵌套里面
补充:可变长参数:1.*args:如果实参是多个位置参数,则*的作用是把多个位置参数打包成元组
         如果实参是序列,则*的作用就是解包
      2.**kwargs:同上,但是关键字参数打包/解包的形式是字典
      例子:
     def test1(a,b,c):
          print(a,b,c)

     def test2(*args): #这个*args=1,2,3;args=(1,2,3)
          test1(*args)

     test2(1,2,3) #打包
     
     def test1(a,b,c):
          print(a,b,c)
     def test2(*args):
          test1(*args)
     test2(*[1,2,3])#解包

#无参装饰器
#用户列表,当然此用户信息也可以由文件形式获取
ulist=[
    {"name":"pzz1","upass":123},
    {"name":"pzz2","upass":123},
    {"name":"pzz3","upass":123}
]
#当前登录的用户的状态。一旦登陆之后,查看其他信息就不要再做验证了.
current_user={"uname":None,"login":False}

def desc(func): #高阶函数
    def test(*args,**kwargs):   #嵌套函数,参数是可变长参数
        if current_user["uname"] and current_user["login"]: #一旦登陆,无需验证
            res = func(*args, **kwargs)
            return res
        else:
            uname=input('username:')
            upass=input('upass:')
            for item in ulist: #遍历用户信息
                if item["name"]=="pzz1" and item["upass"]==123:
                    current_user["uname"]=item["name"]  #修改当前用户状态
                    current_user["login"]=True
                    res=func(*args,**kwargs)    #执行相应操作
                    return res #返回值是一种规范
            else: #正常结束,走else
                print('uname or upass is not correct!')
    return test

@desc #等价于 home=desc(home)
def home(username):
    print("welcome %s to the JDhome"%username)
@desc
def shop_car():
    print("...shop_car")
home("pzz1")
shop_car()

#注意:有参装饰器与无参装饰器的区别

#用户列表,当然此用户信息也可以由文件形式获取
ulist=[
    {"name":"pzz1","upass":123},
    {"name":"pzz2","upass":123},
    {"name":"pzz3","upass":123}
]
#当前登录的用户的状态。一旦登陆之后,查看其他信息就不要再做验证了.
current_user={"uname":None,"login":False}
def yanzhen(type="filedb"):
    def desc(func): #高阶函数
        def test(*args,**kwargs):   #嵌套函数,参数是可变长参数
            print("验证类型是:",type)
            if current_user["uname"] and current_user["login"]: #一旦登陆,无需验证
                res = func(*args, **kwargs)
                return res
            else:
                uname=input('username:')
                upass=input('upass:')
                for item in ulist: #遍历用户信息
                    if item["name"]=="pzz1" and item["upass"]==123:
                        current_user["uname"]=item["name"]  #修改当前用户状态
                        current_user["login"]=True
                        res=func(*args,**kwargs)    #执行相应操作
                        return res #返回值是一种规范
                else: #正常结束,走else
                    print('uname or upass is not correct!')
        return test
    return desc

@yanzhen(type="filedb") #函数执行完毕后,返回desc,@desc等价于 home=desc(home)
def home(username):
    print("welcome %s to the JDhome"%username)
@yanzhen(type="filedb")
def shop_car():
    print("...shop_car")
home("pzz1")
shop_car()


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值