装饰器decorator总结(函数即是变量、高阶函数、嵌套函数、参数组)

装饰器

  1. decorator的本质他就是函数
  2. 原则:--对于被装饰的函数他不发生任何变化,他是透明的。
    • 不能修改被装饰的函数的源代码
    • 不能修改被装饰的函数的调用方式
  3. 函数就是"变量":
    • 普通变量。先定义后使用:如x=1,先定义变量x和再调用变量;函数也如此,先定义函数在调用函数;
python 复制代码
#第一步:定义
def fibo(x):
    if x==0:
        return 1
    elif x==1:
        return 1
    else:
        return fibo(x-1) + fibo(x-2)
#第二步:调用
print(fibo(10))
  • 存储也是一样,函数名以及函数体存储:普通变量x为名牌号,1为值存储在内存中,函数则函数名为名牌号,函数体存储在内存中
  • 匿名函数:特殊的匿名函数没有函数名,用完即回收:lambda x :print(x)
  1. 高阶函数;如def test(x,y,func)
    • 把一个函数当做参数传参给另外一个函数
    • 返回值包含函数名
python 复制代码
def sum(a,b,c):
    return a+b+c

#sum是函数名,为传参
def fun(a,b,c,sum):
    print("count:")
    return sum(a,b,c)  #返回值sum函数名

print(fun(1, 2, 3, sum))
  1. 函数嵌套:定义函数时函数中包含另外一个函数。
python 复制代码
def foo():
    print('in the foo')
    def bar(): #函数foo中的函数
        print('in the bar')
    bar()

foo()
  1. 装饰器=高阶函数+嵌套函数
python 复制代码
def decorator(func):  #func_test01传值给func 高阶函数
    def wrapper(*args,**kwargs):  #嵌套函数 参数组
        start_time = time.time()
        func(*args,**kwargs)  #调用func_test01 参数组
        stop_time = time.time()
        print("the fun runtime is {}".format(stop_time - start_time))
    return wrapper  #返回这个函数的内存地址

@decorator   #func_test01=decorator(func_testo1) 
def fun_test01():  #原函数的源代码不变
    time.sleep(1)
    print('in fun_test01')

fun_test01() #函数调用方式不变
  1. 带参数组的装饰器:三层嵌套
python 复制代码
import time
user_name="agan"
password="123"

def auth(auth_type):
    def outer_wrapper(func):
        print("you auth_type is {}".format(auth_type))
        def wrapper(*args, **kwargs):
            print('wrapper func args:',*args,**kwargs)
            if auth_type == 'local':
                uname = input("Please input your username: ").strip() #去掉字符串开头和结尾的空白字符
                psd = input("Please input your password: ").strip()
                if uname == user_name and psd == password:
                    print("you have authenticated!")
                    res = func(*args, **kwargs)  # 调用func
                    return res
                else:
                    print("wrong username or password!")
            else:
                print("wrong auth type!")
        return wrapper
    return outer_wrapper


def index():
    print("welcome to index page")

@auth(auth_type="local") # home = outer_wrapper
def home():
    print("welcome to home page")

index()
home()
相关推荐
特立独行的猫A2 小时前
用仓颉语言 cj-tauri 框架,五分钟给娃做了个鸿蒙形近字游戏
java·后端·python
大可不必了7 小时前
03 函数定义和调用
开发语言·python
承渊政道8 小时前
【Python编程—从入门到实践】(记分与闯关:用Python+Pygame,打造越战越精彩的太空大战!)
开发语言·python·pycharm·游戏程序·pygame
爱学习的程序媛8 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
imgsq8 小时前
GESP Python 一到四级:备考路线与真题特点
python·青少年编程
eggwyw9 小时前
Anaconda安装卸载的详细流程指南
开发语言·python
2601_9679791210 小时前
纯手写:Python安装pip完整教程,搞了几小时,终于成功
python·教程·pip·安装·包管理
Norna10 小时前
缓存文件从没写出来:一个 .json 被当成目录用了
python
aifannihao12 小时前
AI 学习第 14 课:CNN 训 6 轮卡在 0.5352,数据没打乱
pytorch·python·深度学习·cnn·学习路线
yuyuxun113 小时前
协同过滤的校园二手物品智能推荐系统的设计与实现 毕业设计-附源码40082
javascript·vue.js·spring boot·python·数据分析·django·课程设计