惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
T
Threat Research - Cisco Blogs
Cloudbric
Cloudbric
Recent Commits to openclaw:main
Recent Commits to openclaw:main
I
Intezer
Attack and Defense Labs
Attack and Defense Labs
P
Privacy International News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
V2EX - 技术
V2EX - 技术
AWS News Blog
AWS News Blog
O
OpenAI News
L
LINUX DO - 最新话题
N
News | PayPal Newsroom
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
A
Arctic Wolf
Spread Privacy
Spread Privacy
G
GRAHAM CLULEY
T
Tor Project blog
博客园_首页
Know Your Adversary
Know Your Adversary
有赞技术团队
有赞技术团队
S
Secure Thoughts
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Visual Studio Blog
J
Java Code Geeks
Cisco Talos Blog
Cisco Talos Blog
Schneier on Security
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security Affairs
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
宝玉的分享
宝玉的分享
量子位
Last Week in AI
Last Week in AI
月光博客
月光博客
罗磊的独立博客
S
SegmentFault 最新的问题

博客园 - tsembrace

Pandas数据分析教程,由浅入深讲解(五)Pandas IO操作 Pandas数据分析教程,由浅入深讲解(四) Pandas数据分析教程,由浅入深讲解(三) Pandas数据分析教程,由浅入深讲解(二) Pandas数据分析教程,由浅入深讲解(一) B站黑马Python+AI零基础入门(二)AI应用-实战 B站黑马Python+AI零基础入门(一)AI应用-基础 B站清华128小时Python高级教程一(C3深入类和对象) B站清华128小时Python高级教程一(C1、C2) B站python入门学习---第二阶段第二章数据库、SQL和MySQL B站python入门学习---第二阶段第一章类和对象 B站python入门学习---第一阶段第十章 数据可视化 B站python入门学习---第一阶段第八~九章 文件操作、异常、模块与包 B站python入门学习---第一阶段前七章回顾复习(二) B站python入门学习---第一阶段前七章回顾复习(一) Python入门学习(十)第十四章其他 Python入门学习(九)Python的高级语法与用法(三)函数式编程 Python入门学习(九)Python的高级语法与用法(二)闭包 Python入门学习(九)Python的高级语法与用法(一)枚举 Python入门学习(八)JSON、反序列化、序列化 Python入门学习(七)高级部分:正则表达式(二) Python入门学习(七)高级部分:正则表达式 Python入门学习(六)高级部分:面向对象 Python入门学习(五)函数 Python入门学习(四)流程控制 包与模块
Python入门学习(九)Python的高级语法与用法(四)装饰器
tsembrace · 2025-08-17 · via 博客园 - tsembrace

一、从一个简单问题引入装饰器的作用由来

import time

def f1():
    print(time.time())   #输出unix时间戳
    print("This is the f1 function.")

f1()
#f1()函数很简单,实现一句话的打印
#如果要获取执行函数的时间,则按上述加入time()代码
#如果有很多函数都有增加时间戳的需求,如下面的f2()
def f2():
    print("This is the f2 function.")

#难道都进入函数定义去修改代码吗?
#函数理应对修改是封闭的,对扩展是开放的
#那么按如下思路则可以实现扩展
#将扩展功能单独作为一个函数,将需要扩展功能的原函数作为参数传递进去执行

def print_current_time(func):  #func为需要扩展增加时间戳功能的函数名
    print(time.time())
    func()

#那么对于f2,需要扩展功能,则如下实现
print_current_time(f2)

#但对于上述解决方案,扩展功能单独设置,没有与原函数整合
#本质上,上述print_current_time()函数的实现和下面代码一样
#print(time.time())
#f2()
#那么如何更好地作为原函数的扩展功能,则引入了装饰器这个概念

二、装饰器

import time

#装饰器的基本结构,类似于闭包即函数内部包含函数,并且返回的是函数名
def decorator(func):
    def wrapper():
        print(time.time())
        func()
    return wrapper


def f1():
    print("This is the f1 function.")

f = decorator(f1)
f()

对于上述代码,实现了装饰器的代码,但也并未直接将扩展功能和原始函数绑定关联

仍然需要通过f = decorator(f1)这样的代码去捆绑实现

python中通过@装饰器名实现关联

import time

#装饰器的基本结构,类似于闭包即函数内部包含函数,并且返回的是函数名
def decorator(func):
    def wrapper():
        print(time.time())
        func()
    return wrapper

@decorator
def f1():
    print("This is the f1 function.")

f1()     #等同于f = decorator(f1)     f()

 上述示例中f1()函数是没有参数的,如果带有参数,那么装饰器需要做哪些改动呢?

一个参数的情况:

import time
#一个参数的情况
#装饰器的基本结构,类似于闭包即函数内部包含函数,并且返回的是函数名
def decorator(func):
    def wrapper(fn):
        print(time.time())
        func(fn)
    return wrapper

@decorator
def f1(func_name):
    print("This is the f1 function named " + func_name)

f1("test func")  #等同于f2 = decorator(f1)   f2("test func")

对于同一装饰器,各类参数数量不一的函数都需要用到,则需要用可变参数去实现:

import time
#一个参数的情况
#装饰器的基本结构,类似于闭包即函数内部包含函数,并且返回的是函数名
def decorator(func):
    def wrapper(*args):
        print(time.time())
        func(*args)
    return wrapper

@decorator
def f1(func_name):
    print("This is the f1 function named " + func_name)

@decorator
def f2(a, b):
    print("a + b = " + str(a+b))
    print("This is the f2 function!")

f1("TEST FUNC")
f2(30, 50)

进一步如果涉及到关键字可变参数,则如下:

import time
#一个参数的情况
#装饰器的基本结构,类似于闭包即函数内部包含函数,并且返回的是函数名
def decorator(func):
    def wrapper(*args, **kw):
        print(time.time())
        func(*args, **kw)
    return wrapper

@decorator
def f1(func_name):
    print("This is the f1 function named " + func_name)

@decorator
def f2(a, b):
    print("a + b = " + str(a+b))
    print("This is the f2 function!")

@decorator
def f3(func_name1, func_name2, **kw):
    print("This is the f3 function " + func_name1)
    print("This is the f3 function " + func_name2)
    for k, v in kw.items():
        print(k, ":", v)

f1("func1 name")
f2(12, 38)
f3("test func1", "test func2", a = 1, b = 2, c = "ABC")