【python基础知识】装饰器

【python基础知识】装饰器

1. 概念

装饰器的本质是函数,(装饰其他的函数)就是为其他函数添加附加的功能

原则: 1.不能修改被装饰的函数的源代码。

​ 2.不能修改呗装饰的函数的调用方

简单的装饰器,直观的观察装饰器的作用跟写法。

# Author:Li xp

import time
#装饰器
def timmer(func):
    def warpper(*args,**kwargs):
        start_time = time.time()
        func()
        stop_time = time .time()
        print("in the func run time is  %s" %(stop_time-start_time))

    return warpper()

@timmer
def test1():
    time.sleep(3)
    print("in the test1")

#函数调用
test1()
  1. 知识储备

学习装饰器所需要的知识储备:

1.函数即“变量”(在匿名函数中可以更直观的看出来。)

2.高阶函数

3.嵌套函数

高阶函数+嵌套函数====》装饰器

  1. 函数(在内存中)

在内存中的解释!
在这里插入图片描述

  1. 高阶函数

​ a:把一个函数名当做实参传给另外一个函数

​ b:返回值中包含函数名

​ c:返回值中也可以包含函数

# Author:Li xp
def bar():
    print("in the bar")

def test1(func):
    print(func)
#这个func是函数的地址在这里等bar更好理解,bar()这样就是调用函数里面的内容
    func()
test1(bar)
#把第一个函数名传给第二个函数打印出来,则打印出来的是传入函数的地址

func=bar

这里很直观的看出来高阶函数的根本,在第二个函数中找到的是第一个函数的地址,从而取出其中的内容。

  1. 嵌套函数

2. 形式

  1. 装饰器part1
# Author:Li xp
import time

def timeer(func):
    def deco():
        start_time=time.time()
        func()
        stop_time=time.time()
        print("其中函数运行时间为%s" %(stop_time-start_time))
    return deco
#
# def timeer():
#     def deco():
#         pass
@timeer
def test1():
    time.sleep(3)
    print("in the test1")
@timeer
def test2():
    time.sleep(3)
    print("in the test2")

# test1()
# test2()
#这样调用就可以运行其中两个!
#test1=timeer(test1)
test1()
#test2=timeer(test2)
test2()
# Author:Li xp
import time

def timeer(func):
    def deco():
        start_time=time.time()
        func()
        stop_time=time.time()
        print("其中函数运行时间为%s" %(stop_time-start_time))
    return deco
#这里给一个嵌套函数的末班
# def timeer():
#     def deco():
#         pass
@timeer
def test1():
    time.sleep(3)
    print("in the test1")
@timeer
def test2():
    time.sleep(3)
    print("in the test2")

# test1()
# test2()
#这样调用就可以运行其中两个!
#test1=timeer(test1)
test1()
#test2=timeer(test2)
test2()

在看上述代码时一定要注意是怎么一步一步走到最后的这种结果。#test1=timeer(test1)在上述代码中@timeer就等于前面的这条语句

3.修改完善

(在装饰器函数中加入两个不固定的参数!)

def timeer(func):
    def deco(*arg1,**kwargs):
        start_time=time.time()
        func(*arg1,**kwargs)
        stop_time=time.time()
        print("其中函数运行时间为%s" %(stop_time-start_time))
    return deco

4.补充一个带参数一个带返回值的装饰器

# Author:Li xp
import time
user,passwd="lxp","lxp123456"
def auth(func):
    def wrapper(*args,**kwargs):
        username = input("Username:").strip()
        password = input("password:").strip()

        if user  == username and passwd == password:
            print("\033[32;1m用户成功登录\033[0m")
            res = func(*args,**kwargs)
            print("------------=after authenticaion")
            return res
        else:
            exit("\033[32;1m用户验证失败\033[0m")
    return wrapper

def index():
    print("欢迎进入index页面")
@auth
def home():
    print("欢迎进入home页面")
    return "from home"
def bbs():
    print("欢迎进入bbs页面")

index()
print(home())
bbs()

5.设计具有判断功能的装饰器部分

# Author:Li xp
import time
user,passwd="lxp","lxp123456"
def auth(auth_type):
    print("auth func:",auth_type)
    def outer_wrapper(func):
        def wrapper(*args,**kwargs):
            print("wrapper func:", *args,**kwargs)
            if auth_type == "local":
                username = input("Username:").strip()
                password = input("password:").strip()
                if user  == username and passwd == password:
                    print("\033[32;1m用户成功登录\033[0m")
                    res = func(*args,**kwargs)
                    print("------------=after authenticaion")
                    return res
                else:
                    exit("\033[32;1m用户验证失败\033[0m")
            elif auth_type == "ldap":
                print("不会啊打印个啥东西")
        return wrapper
    return outer_wrapper
def index():
    print("欢迎进入index页面")
#认证实用本地的文件认证
@auth(auth_type="local")
def home():
    print("欢迎进入home页面")
    return "from home"
#实用远程的Http认证
@auth(auth_type="ldap")
def bbs():
    print("欢迎进入bbs页面")

index()
print(home())
bbs()

t(“欢迎进入index页面”)
#认证实用本地的文件认证
@auth(auth_type=“local”)
def home():
print(“欢迎进入home页面”)
return “from home”
#实用远程的Http认证
@auth(auth_type=“ldap”)
def bbs():
print(“欢迎进入bbs页面”)

index()
print(home())
bbs()








猜你喜欢

转载自blog.csdn.net/weixin_45571972/article/details/109105176