<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>元编程 on 老张开工了</title>
    <link>/%E5%85%83%E7%BC%96%E7%A8%8B/</link>
    <description>Recent content in 元编程 on 老张开工了</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Thu, 21 May 2026 00:00:00 +0000</lastBuildDate>
    <atom:link href="/%E5%85%83%E7%BC%96%E7%A8%8B/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>装饰器深入与实践</title>
      <link>/posts/2026/05/decorators/</link>
      <pubDate>Thu, 21 May 2026 00:00:00 +0000</pubDate>
      <guid>/posts/2026/05/decorators/</guid>
      <description>认识装饰器 装饰器（Decorator）是 Python 中一种强大的元编程工具。简单来说，装饰器就是一个函数，它接受另一个函数作为参数，并返回一个新的函数。装饰器允许我们在不修改原函数源代码的情况下，给函数添加额外的功能。&#xA;函数是一等公民 要理解装饰器，先要理解 Python 中&amp;quot;函数是一等公民&amp;quot;这个概念：&#xA;# 函数可以赋值给变量 def greet(name): return f&amp;#34;你好, {name}!&amp;#34; say_hello = greet print(say_hello(&amp;#34;小明&amp;#34;)) # 输出: 你好, 小明! # 函数可以作为参数传递 def call_twice(func, arg): return func(arg), func(arg) result1, result2 = call_twice(greet, &amp;#34;小红&amp;#34;) print(result1) # 输出: 你好, 小红! # 函数可以嵌套定义 def outer(): def inner(): return &amp;#34;我是内部函数&amp;#34; return inner inner_func = outer() print(inner_func()) # 输出: 我是内部函数 最简单的装饰器 基于上述特性，我们可以写出最简单的装饰器：&#xA;def my_decorator(func): def wrapper(): print(&amp;#34;函数调用前执行&amp;#34;) func() print(&amp;#34;函数调用后执行&amp;#34;) return wrapper def say_hello(): print(&amp;#34;你好!</description>
    </item>
  </channel>
</rss>
