装饰器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()
相关推荐
小oo呆6 分钟前
【学习心得】Python协程
python
信誓旦旦的程序猿21 分钟前
【Python量化实战宝典 #22】涨跌停、连板情绪手册:情绪指标构建(Python 可跑)
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
gCode Teacher 格码致知26 分钟前
Pandas教学-21:subplots的使用
python·pandas·1024程序员节
2601_9679801130 分钟前
Python开发的顶级开源项目全景图:2025-2026年生态概览
python·机器学习·开源项目·数据科学·生态概览
benchmark_cc1 小时前
量化开发中,什么时候应该停止手动下载股票数据?
python·数据分析·pandas·量化交易·1024程序员节·股票数据·quantdash
泡茶喝茶写代码1 小时前
A股量化策略回测实战:从行情数据到绩效评估(第 15 篇):新高新低策略
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
全栈练习生2 小时前
Jev 决策模型
python·ai·1024程序员节
LMT流氓兔2 小时前
第五单元——第十二课:把脚本整理成项目
java·数据库·python·langchain·1024程序员节
光依旧2 小时前
day-trader-skill实测:让Claude Code当交易助理,纯Python+确定性+fail-closed
python·量化交易·ai agent·a股·风控·claude code·开源项目实测
CCblandy3 小时前
前缀缓存命中率不靠引擎靠排列:本机仿真 3 轮,易变字段放开头 98.0%→0.0%
人工智能·python·算法·自然语言处理·性能优化