news 2026/3/11 23:07:51

Python 开发 - Python 装饰器(装饰器概述、函数概念、装饰器手动实现、装饰器语法糖实现)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python 开发 - Python 装饰器(装饰器概述、函数概念、装饰器手动实现、装饰器语法糖实现)

一、装饰器概述

  1. 装饰器允许开发者在不修改原函数代码的情况下,给函数添加额外的功能

  2. 装饰器本质上是一个返回函数的高阶函数

  3. 在 Python 中,使用装饰器语法糖@可以便捷应用装饰器


二、函数概念

1、函数是一等对象
  1. 函数可以赋值给变量
defgreet(name):returnf"Hello,{name}"my_func=greetprint(my_func("Alice"))
# 输出结果 Hello, Alice
  1. 函数可以作为参数传递
defgreet(name):print(f"Hello,{name}")defcall_twice(func,arg):greet(arg)greet(arg)call_twice(greet,"Alice")
# 输出结果 Hello, Alice Hello, Alice
  1. 可以定义在另一个函数内部
defcall_twice(arg):defgreet(name):returnf"Hello,{name}"print(greet(arg)+" "+greet(arg))call_twice("Alice")
# 输出结果 Hello, Alice Hello, Alice
  1. 函数可以作为返回值
defget_func(flag):defadd(num1,num2):returnnum1+num2defsubtract(num1,num2):returnnum1-num2ifflag=="+":returnaddelifflag=="-":returnsubtract result_func=get_func("+")result=result_func(10,20)print(result)
# 输出结果 30
2、闭包
  • 闭包是嵌套函数中,内部函数引用外部函数的变量,即使外部函数已经执行完毕

  • 如下例,函数 inner_func 引用了外部函数的变量 x,即使函数 outer_func 已经执行完,函数 closure 仍能访问 x

defouter_func(x):definner_func(y):returnx+yreturninner_func closure=outer_func(10)result=closure(5)print(result)
# 输出结果 15

三、装饰器手动实现

  1. 基本实现
# 装饰器函数defmy_decorator(func):defwrapper():print("函数执行前")result=func()print("函数执行后")returnresultreturnwrapper# 原始函数defsay_hello():print("Hello")# 应用装饰器decorated_say_hello=my_decorator(say_hello)decorated_say_hello()
# 输出结果 函数执行前 Hello 函数执行后
  1. 函数带参数
# 装饰器函数defmy_decorator(func):defwrapper(**kwargs):print("函数执行前")result=func(**kwargs)print("函数执行后")returnresultreturnwrapper# 原始函数defsay_hello(name):print(f"Hello,{name}")# 应用装饰器decorated_say_hello=my_decorator(say_hello)decorated_say_hello(name="Alice")
# 输出结果 函数执行前 Hello, Alice 函数执行后
  1. 装饰器带参数
# 装饰器函数defmy_decorator(func,times):defwrapper():print("函数执行前")foriinrange(times):func()print("函数执行后")returnwrapper# 原始函数defsay_hello():print("Hello World")# 应用装饰器decorated_say_hello=my_decorator(say_hello,3)decorated_say_hello()
# 输出结果 函数执行前 Hello World Hello World Hello World 函数执行后

四、装饰器语法糖实现

  1. 基本实现
# 装饰器函数defmy_decorator(func):defwrapper():print("函数执行前")result=func()print("函数执行后")returnresultreturnwrapper# 应用装饰器@my_decoratordefsay_hello():print("Hello")say_hello()
# 输出结果 函数执行前 Hello 函数执行后
  1. 函数带参数
# 装饰器函数defmy_decorator(func):defwrapper(**kwargs):print("函数执行前")result=func(**kwargs)print("函数执行后")returnresultreturnwrapper# 应用装饰器@my_decoratordefsay_hello(name):print(f"Hello,{name}")say_hello(name="Alice")
# 输出结果 函数执行前 Hello, Alice 函数执行后
  1. 装饰器带参数
# 装饰器函数defmy_decorator(times):defdecorator(func):defwrapper():print("函数执行前")foriinrange(times):func()print("函数执行后")returnwrapperreturndecorator# 应用装饰器@my_decorator(times=3)defsay_hello():print("Hello World")say_hello()
# 输出结果 函数执行前 Hello World Hello World Hello World 函数执行后
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/11 9:19:19

5分钟上手!Gource代码可视化工具:让项目历史动起来

5分钟上手!Gource代码可视化工具:让项目历史动起来 【免费下载链接】Gource software version control visualization 项目地址: https://gitcode.com/gh_mirrors/go/Gource 你是否好奇过代码仓库的演变历程?想不想把枯燥的提交记录变…

作者头像 李华
网站建设 2026/3/3 18:04:59

31、Ubuntu网络配置全攻略

Ubuntu网络配置全攻略 1. 网络配置工具概述 在Ubuntu系统中,当添加或更换网络硬件后,需要对新硬件进行配置。可以通过命令行或图形化配置工具来完成。对于Linux新手而言,使用命令行配置工具可能有一定难度,而 nm - connection - editor 图形化工具则是更好的选择。不过…

作者头像 李华
网站建设 2026/3/4 1:17:33

F5-TTS终极部署指南:从零开始构建专业级语音合成系统

F5-TTS终极部署指南:从零开始构建专业级语音合成系统 【免费下载链接】F5-TTS Official code for "F5-TTS: A Fairytaler that Fakes Fluent and Faithful Speech with Flow Matching" 项目地址: https://gitcode.com/gh_mirrors/f5/F5-TTS 还在为…

作者头像 李华
网站建设 2026/3/10 12:34:55

收藏!2025中国大模型市场全景解析:规模破290亿,竞争梯队+核心玩家一文看懂(小白程序员必学)

最新行业数据显示,2024年中国大模型市场迎来爆发式增长,整体规模已达到294.16亿元,其中多模态大模型成为核心增长引擎,贡献了156.3亿元的市场份额,数字人交互、游戏场景革新、智能办公升级等下游应用场景增长势头尤为迅…

作者头像 李华