{"id":49116,"date":"2025-07-30T15:46:46","date_gmt":"2025-07-30T07:46:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/49116.html"},"modified":"2025-07-30T15:46:46","modified_gmt":"2025-07-30T07:46:46","slug":"1%ef%b8%8f%e2%83%a32%ef%b8%8f%e2%83%a3-%e8%a3%85%e9%a5%b0%e5%99%a8%ef%bc%88decorator%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/49116.html","title":{"rendered":"1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09"},"content":{"rendered":"<p>\u5728 Python \u4e2d&#xff0c;\u6709\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u7279\u6027\u2014\u2014\u88c5\u9970\u5668&#xff08;Decorator&#xff09;\u3002\u5b83\u80fd\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u6e90\u4ee3\u7801\u7684\u524d\u63d0\u4e0b&#xff0c;\u52a8\u6001\u5730\u4e3a\u51fd\u6570\u589e\u52a0\u529f\u80fd&#xff0c;\u6bd4\u5982\u6253\u5370\u65e5\u5fd7\u3001\u9a8c\u8bc1\u6743\u9650\u3001\u8ba1\u65f6\u7edf\u8ba1\u7b49\u3002\u8fd9\u79cd&#034;\u65e0\u4fb5\u5165\u5f0f&#034;\u589e\u5f3a\u529f\u80fd\u7684\u65b9\u5f0f&#xff0c;\u8ba9\u4ee3\u7801\u66f4\u7075\u6d3b\u3001\u66f4\u6613\u7ef4\u62a4\u3002\u4eca\u5929\u6211\u4eec\u5c31\u6765\u6df1\u5165\u7406\u89e3\u88c5\u9970\u5668\u7684\u5de5\u4f5c\u539f\u7406\u548c\u7528\u6cd5\u3002 \u901a\u4fd7\u7248&#xff1a;<\/p>\n<p>Def xx1<span class=\"token punctuation\">(<\/span>\u88ab\u88c5\u9970\u51fd\u6570&#xff09;&#xff1a;<br \/>\n     def xx2&#xff08;\u5982\u679c\u88ab\u88c5\u9970\u51fd\u6570\u6709\u53c2\u6570\u90a3\u4e48\u8f93\u5165&#xff09;&#xff1a;<br \/>\n             xxxxxxxxxxxxxxx<br \/>\n             \u88ab\u88c5\u9970\u51fd\u6570&#xff08;\u5982\u679c\u88ab\u88c5\u9970\u51fd\u6570\u6709\u53c2\u6570\u90a3\u4e48\u8f93\u5165&#xff09;<br \/>\n             xxxxxxxxxxxxxxx<br \/>\n             <span class=\"token comment\"># \u5982\u679c\u88ab\u88c5\u9970\u51fd\u6570\u4e2d\u542b\u6709reture\u5219\u9700\u8981\u8fd4\u56de\u88ab\u88c5\u9970\u51fd\u6570<\/span><br \/>\n             <span class=\"token comment\"># \u6ca1\u6709\u5219\u4e0d\u9700\u8981<\/span><br \/>\n       reture xx2<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>\u4e00\u3001 \u4e3a\u4ec0\u4e48\u9700\u8981\u88c5\u9970\u5668&#xff1f;<\/li>\n<li>\n<ul>\n<li>&#x1f9ea; \u88c5\u9970\u5668\u57fa\u7840&#xff1a;\u4ece\u51fd\u6570\u8d4b\u503c\u8bf4\u8d77<\/li>\n<\/ul>\n<\/li>\n<li>\u4e8c\u3001 \u5b9a\u4e49\u7b2c\u4e00\u4e2a\u88c5\u9970\u5668&#xff1a;\u65e5\u5fd7\u589e\u5f3a<\/li>\n<li>\n<ul>\n<li>1. \u57fa\u7840\u88c5\u9970\u5668\u7ed3\u6784<\/li>\n<li>2. \u4f7f\u7528\u88c5\u9970\u5668&#xff1a;&#064;\u8bed\u6cd5\u7cd6<\/li>\n<li>3. \u539f\u7406\u62c6\u89e3&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001 \u8fdb\u9636&#xff1a;\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668<\/li>\n<li>\n<ul>\n<li>1. \u5b9a\u4e49\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668<\/li>\n<li>2. \u4f7f\u7528\u65b9\u5f0f<\/li>\n<li>3. \u8c03\u7528\u6548\u679c<\/li>\n<\/ul>\n<\/li>\n<li>\u56db\u3001 \u6ce8\u610f&#xff1a;\u51fd\u6570\u8eab\u4efd\u4fe1\u606f\u7684\u4e22\u5931<\/li>\n<li>\u4e94\u3001 \u6838\u5fc3\u77e5\u8bc6\u5c0f\u7ed3<\/li>\n<li>\u516d\u3001 \u7ec3\u4e60<\/li>\n<li>\n<ul>\n<li>&#x1f9e9; \u7ec3\u4e601&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u8ba1\u65f6\u88c5\u9970\u5668<\/li>\n<li>&#x1f9e9; \u7ec3\u4e602&#xff1a;\u5b9e\u73b0\u517c\u5bb9\u4e24\u79cd\u5f62\u5f0f\u7684\u88c5\u9970\u5668<\/li>\n<\/ul>\n<\/li>\n<li>\u4e03\u3001 \u603b\u7ed3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u4e00\u3001 \u4e3a\u4ec0\u4e48\u9700\u8981\u88c5\u9970\u5668&#xff1f;<\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6253\u5370\u65e5\u671f\u7684\u51fd\u6570&#xff1a;<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">now<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;2024-6-1&#039;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u73b0\u5728\u9700\u8981\u7ed9\u5b83\u52a0\u4e00\u4e2a\u529f\u80fd&#xff1a;\u8c03\u7528\u524d\u81ea\u52a8\u6253\u5370\u65e5\u5fd7&#xff08;\u6bd4\u5982\u201c\u5f00\u59cb\u8c03\u7528 now()\u201d&#xff09;\u3002\u5982\u679c\u76f4\u63a5\u4fee\u6539 now() \u7684\u4ee3\u7801&#xff0c;\u4f1a\u6709\u4e24\u4e2a\u95ee\u9898&#xff1a;<\/p>\n<ul>\n<li>\u591a\u4e2a\u51fd\u6570\u9700\u8981\u52a0\u65e5\u5fd7\u65f6&#xff0c;\u91cd\u590d\u4ee3\u7801\u4f1a\u975e\u5e38\u7e41\u7410&#xff1b;<\/li>\n<li>\u7834\u574f\u539f\u51fd\u6570\u7684\u5c01\u88c5\u6027&#xff0c;\u540e\u7eed\u7ef4\u62a4\u96be\u5ea6\u589e\u52a0\u3002<\/li>\n<\/ul>\n<p>\u88c5\u9970\u5668\u7684\u51fa\u73b0\u5b8c\u7f8e\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898&#xff1a;\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u524d\u63d0\u4e0b&#xff0c;\u52a8\u6001\u589e\u5f3a\u51fd\u6570\u529f\u80fd\u3002<\/p>\n<h4>&#x1f9ea; \u88c5\u9970\u5668\u57fa\u7840&#xff1a;\u4ece\u51fd\u6570\u8d4b\u503c\u8bf4\u8d77<\/h4>\n<p>\u8981\u7406\u89e3\u88c5\u9970\u5668&#xff0c;\u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u7279\u6027&#xff1a;\u51fd\u6570\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf&#xff0c;\u901a\u8fc7\u53d8\u91cf\u8c03\u7528\u3002<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">now<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;2024-6-1&#039;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>f <span class=\"token operator\">&#061;<\/span> now  <span class=\"token comment\"># \u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf&#xff08;\u6ce8\u610f\u6ca1\u6709\u62ec\u53f7&#xff09;<\/span><br \/>\nf<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8c03\u7528\u53d8\u91cf&#xff0c;\u7b49\u4ef7\u4e8e now() \u2192 \u8f93\u51fa&#xff1a;2024-6-1<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u51fd\u6570\u5bf9\u8c61\u7684__name__\u5c5e\u6027 \u2192 \u8f93\u51fa&#xff1a;now<\/span><\/p>\n<p>\u8fd9\u4e00\u7279\u6027\u662f\u88c5\u9970\u5668\u7684\u57fa\u7840\u2014\u2014\u88c5\u9970\u5668\u672c\u8d28\u662f\u4e00\u4e2a\u63a5\u6536\u51fd\u6570\u3001\u8fd4\u56de\u65b0\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001 \u5b9a\u4e49\u7b2c\u4e00\u4e2a\u88c5\u9970\u5668&#xff1a;\u65e5\u5fd7\u589e\u5f3a<\/h3>\n<p>\u88c5\u9970\u5668\u672c\u8d28\u662f\u4e00\u4e2a\u8fd4\u56de\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570\u3002\u5b83\u63a5\u6536\u539f\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u5305\u88c5\u540e\u7684\u65b0\u51fd\u6570&#xff0c;\u65b0\u51fd\u6570\u4f1a\u5728\u8c03\u7528\u539f\u51fd\u6570\u7684\u524d\u540e\u6dfb\u52a0\u989d\u5916\u903b\u8f91\u3002<\/p>\n<h4>1. \u57fa\u7840\u88c5\u9970\u5668\u7ed3\u6784<\/h4>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u5b9a\u4e49\u5305\u88c5\u51fd\u6570wrapper&#xff0c;\u7528\u4e8e\u589e\u5f3a\u539f\u51fd\u6570<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">wrapper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u8c03\u7528\u539f\u51fd\u6570\u524d&#xff1a;\u6dfb\u52a0\u65e5\u5fd7\u529f\u80fd<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#039;call <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>func<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">():&#039;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token comment\"># \u8c03\u7528\u539f\u51fd\u6570&#xff0c;\u5e76\u8fd4\u56de\u7ed3\u679c<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> func<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u8fd4\u56de\u5305\u88c5\u540e\u7684\u51fd\u6570<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> wrapper<\/p>\n<h4>2. \u4f7f\u7528\u88c5\u9970\u5668&#xff1a;&#064;\u8bed\u6cd5\u7cd6<\/h4>\n<p>\u7528 &#064;log \u6807\u8bb0\u9700\u8981\u589e\u5f3a\u7684\u51fd\u6570&#xff0c;\u7b49\u4ef7\u4e8e now &#061; log(now)&#xff1a;<\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;log<\/span>  <span class=\"token comment\"># \u7b49\u4ef7\u4e8e now &#061; log(now)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">now<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;2024-6-1&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    now<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u8f93\u51fa&#xff1a;<\/span><br \/>\n<span class=\"token comment\"># call now():  # \u88c5\u9970\u5668\u65b0\u589e\u7684\u65e5\u5fd7<\/span><br \/>\n<span class=\"token comment\"># 2024-6-1     # \u539f\u51fd\u6570\u7684\u529f\u80fd<\/span><\/p>\n<h4>3. \u539f\u7406\u62c6\u89e3&#xff1a;<\/h4>\n<li>&#064;log \u4f1a\u5c06 now \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9 log&#xff1b;<\/li>\n<li>log \u51fd\u6570\u8fd4\u56de wrapper \u51fd\u6570&#xff0c;\u6b64\u65f6 now \u53d8\u91cf\u6307\u5411 wrapper&#xff1b;<\/li>\n<li>\u8c03\u7528 now() \u5b9e\u9645\u662f\u8c03\u7528 wrapper()&#xff0c;\u5148\u6267\u884c\u65e5\u5fd7\u903b\u8f91&#xff0c;\u518d\u8c03\u7528\u539f now()\u3002<\/li>\n<p>wrapper(*args, **kw) \u4e2d\u7684 *args, **kw \u662f\u4e3a\u4e86\u63a5\u6536\u539f\u51fd\u6570\u7684\u4efb\u610f\u53c2\u6570&#xff0c;\u4fdd\u8bc1\u65e0\u8bba\u539f\u51fd\u6570\u6709\u591a\u5c11\u53c2\u6570&#xff0c;\u88c5\u9970\u5668\u90fd\u80fd\u517c\u5bb9\u3002<\/p>\n<h3>\u4e09\u3001 \u8fdb\u9636&#xff1a;\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668<\/h3>\n<p>\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u65e5\u5fd7\u5185\u5bb9&#xff08;\u6bd4\u5982\u6253\u5370\u201c\u6267\u884c\u201d\u6216\u201c\u8c03\u7528\u201d&#xff09;&#xff0c;\u88c5\u9970\u5668\u9700\u8981\u4e09\u5c42\u5d4c\u5957&#xff1a;\u6700\u5916\u5c42\u63a5\u6536\u53c2\u6570&#xff0c;\u4e2d\u95f4\u5c42\u63a5\u6536\u539f\u51fd\u6570&#xff0c;\u5185\u5c42\u662f\u5305\u88c5\u51fd\u6570\u3002<\/p>\n<h4>1. \u5b9a\u4e49\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668<\/h4>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span>text<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>  <span class=\"token comment\"># \u6700\u5916\u5c42&#xff1a;\u63a5\u6536\u88c5\u9970\u5668\u53c2\u6570<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">decorator<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>  <span class=\"token comment\"># \u4e2d\u95f4\u5c42&#xff1a;\u63a5\u6536\u539f\u51fd\u6570<\/span><br \/>\n        <span class=\"token keyword\">def<\/span> <span class=\"token function\">wrapper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span>  <span class=\"token comment\"># \u5185\u5c42&#xff1a;\u5305\u88c5\u51fd\u6570<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#039;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>text<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>func<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">():&#039;<\/span><\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u4f7f\u7528\u53c2\u6570text<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> func<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> wrapper  <span class=\"token comment\"># \u8fd4\u56de\u5305\u88c5\u51fd\u6570<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> decorator  <span class=\"token comment\"># \u8fd4\u56de\u4e2d\u95f4\u5c42\u51fd\u6570<\/span><\/p>\n<p>\u901a\u4fd7\u7248&#xff1a;<\/p>\n<p>def log<span class=\"token punctuation\">(<\/span>text<span class=\"token punctuation\">)<\/span>:  <span class=\"token comment\"># \u5916\u5c42&#xff1a;\u63a5\u6536\u88c5\u9970\u5668\u7684\u53c2\u6570&#xff0c;\u6bd4\u5982 &#039;execute&#039;<\/span><\/p>\n<p>    def \u771f\u6b63\u88c5\u9970\u5668<span class=\"token punctuation\">(<\/span>\u88ab\u88c5\u9970\u7684\u51fd\u6570<span class=\"token punctuation\">)<\/span>:  <span class=\"token comment\"># \u4e2d\u5c42&#xff1a;\u63a5\u6536\u88ab\u88c5\u9970\u7684\u51fd\u6570<\/span><\/p>\n<p>        def \u5305\u88c5\u51fd\u6570<span class=\"token punctuation\">(<\/span>*args, **kwargs<span class=\"token punctuation\">)<\/span>:  <span class=\"token comment\"># \u5185\u5c42&#xff1a;\u63a5\u6536\u51fd\u6570\u8fd0\u884c\u65f6\u7684\u53c2\u6570<\/span><br \/>\n            print<span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#039;{text} {\u88ab\u88c5\u9970\u7684\u51fd\u6570.__name__}():&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6253\u5370\u65e5\u5fd7\u4fe1\u606f<\/span><br \/>\n            <span class=\"token builtin class-name\">return<\/span> \u88ab\u88c5\u9970\u7684\u51fd\u6570<span class=\"token punctuation\">(<\/span>*args, **kwargs<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8c03\u7528\u539f\u51fd\u6570&#xff0c;\u5e76\u8fd4\u56de\u7ed3\u679c&#xff08;\u5982\u679c\u539f\u51fd\u6570\u6709\u8fd4\u56de\u503c&#xff09;<\/span><\/p>\n<p>        <span class=\"token builtin class-name\">return<\/span> \u5305\u88c5\u51fd\u6570  <span class=\"token comment\"># \u4e2d\u5c42\u8fd4\u56de\u5305\u88c5\u540e\u7684\u51fd\u6570<\/span><\/p>\n<p>    <span class=\"token builtin class-name\">return<\/span> \u771f\u6b63\u88c5\u9970\u5668  <span class=\"token comment\"># \u5916\u5c42\u8fd4\u56de\u88c5\u9970\u5668<\/span><\/p>\n<h4>2. \u4f7f\u7528\u65b9\u5f0f<\/h4>\n<p><span class=\"token decorator annotation punctuation\">&#064;log<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;execute&#039;<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u7b49\u4ef7\u4e8e now &#061; log(&#039;execute&#039;)(now)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">now<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;2024-6-1&#039;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h4>3. \u8c03\u7528\u6548\u679c<\/h4>\n<p>now<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u8f93\u51fa&#xff1a;<\/span><br \/>\n<span class=\"token comment\"># execute now():  # \u81ea\u5b9a\u4e49\u7684\u65e5\u5fd7\u6587\u672c<\/span><br \/>\n<span class=\"token comment\"># 2024-6-1<\/span><\/p>\n<h3>\u56db\u3001 \u6ce8\u610f&#xff1a;\u51fd\u6570\u8eab\u4efd\u4fe1\u606f\u7684\u4e22\u5931<\/h3>\n<p>\u88c5\u9970\u5668\u4f1a\u6539\u53d8\u539f\u51fd\u6570\u7684\u5143\u4fe1\u606f&#xff08;\u5982 __name__ \u5c5e\u6027&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>now<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8f93\u51fa&#xff1a;wrapper&#xff08;\u800c\u975e\u539f\u51fd\u6570\u540dnow&#xff09;<\/span><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a now \u5df2\u6307\u5411 wrapper \u51fd\u6570\u3002\u4f46\u6709\u4e9b\u4ee3\u7801\u4f9d\u8d56\u51fd\u6570\u7684\u539f\u59cb\u540d\u79f0&#xff08;\u6bd4\u5982\u5e8f\u5217\u5316\u3001\u8c03\u8bd5\u5de5\u5177&#xff09;&#xff0c;\u8fd9\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u7528 functools.wraps \u590d\u5236\u539f\u51fd\u6570\u7684\u5143\u4fe1\u606f\u3002<\/p>\n<p>\u2705 \u6b63\u786e\u5199\u6cd5&#xff1a;\u4fdd\u7559\u539f\u51fd\u6570\u4fe1\u606f<\/p>\n<p><span class=\"token keyword\">import<\/span> functools<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token decorator annotation punctuation\">&#064;functools<span class=\"token punctuation\">.<\/span>wraps<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u590d\u5236\u539f\u51fd\u6570\u5143\u4fe1\u606f\u5230wrapper<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">wrapper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#039;call <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>func<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">():&#039;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> func<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> wrapper<\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;log<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">now<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;2024-6-1&#039;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>now<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8f93\u51fa&#xff1a;now&#xff08;\u6b63\u786e\u4fdd\u7559\u539f\u51fd\u6570\u540d&#xff09;<\/span><\/p>\n<p>\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\u4e5f\u9700\u8981\u540c\u6837\u5904\u7406&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> functools<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span>text<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">decorator<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token decorator annotation punctuation\">&#064;functools<span class=\"token punctuation\">.<\/span>wraps<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5173\u952e&#xff1a;\u590d\u5236\u5143\u4fe1\u606f<\/span><br \/>\n        <span class=\"token keyword\">def<\/span> <span class=\"token function\">wrapper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#039;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>text<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>func<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">():&#039;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> func<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> wrapper<br \/>\n    <span class=\"token keyword\">return<\/span> decorator<\/p>\n<h3>\u4e94\u3001 \u6838\u5fc3\u77e5\u8bc6\u5c0f\u7ed3<\/h3>\n<table>\n<tr>\u6982\u5ff5\u8bf4\u660e\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>\u88c5\u9970\u5668\u672c\u8d28<\/td>\n<td>\u8fd4\u56de\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570<\/td>\n<td>def log(func): return wrapper<\/td>\n<\/tr>\n<tr>\n<td>&#064;\u8bed\u6cd5\u7cd6<\/td>\n<td>\u7b49\u4ef7\u4e8e \u51fd\u6570 &#061; \u88c5\u9970\u5668(\u51fd\u6570)<\/td>\n<td>&#064;log \u2192 now &#061; log(now)<\/td>\n<\/tr>\n<tr>\n<td>\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668<\/td>\n<td>\u4e09\u5c42\u5d4c\u5957&#xff0c;\u6700\u5916\u5c42\u63a5\u6536\u53c2\u6570<\/td>\n<td>&#064;log(&#039;execute&#039;)<\/td>\n<\/tr>\n<tr>\n<td>functools.wraps<\/td>\n<td>\u4fdd\u7559\u539f\u51fd\u6570\u7684__name__\u3001\u6587\u6863\u7b49\u4fe1\u606f<\/td>\n<td>&#064;functools.wraps(func)<\/td>\n<\/tr>\n<tr>\n<td>wrapper\u53c2\u6570\u8bbe\u8ba1<\/td>\n<td>\u7528*args, **kw\u517c\u5bb9\u4efb\u610f\u51fd\u6570\u53c2\u6570<\/td>\n<td>def wrapper(*args, **kw):<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u516d\u3001 \u7ec3\u4e60<\/h3>\n<h4>&#x1f9e9; \u7ec3\u4e601&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u8ba1\u65f6\u88c5\u9970\u5668<\/h4>\n<p>\u5c1d\u8bd5\u5199\u4e00\u4e2a\u88c5\u9970\u5668&#xff0c;\u7edf\u8ba1\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> time<br \/>\n<span class=\"token keyword\">import<\/span> functools<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">timer<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token decorator annotation punctuation\">&#064;functools<span class=\"token punctuation\">.<\/span>wraps<\/span><span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">wrapper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        start <span class=\"token operator\">&#061;<\/span> time<span class=\"token punctuation\">.<\/span>time<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        result <span class=\"token operator\">&#061;<\/span> func<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kw<span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8c03\u7528\u539f\u51fd\u6570<\/span><br \/>\n        end <span class=\"token operator\">&#061;<\/span> time<span class=\"token punctuation\">.<\/span>time<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#039;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>func<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> \u6267\u884c\u65f6\u95f4&#xff1a;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>end <span class=\"token operator\">&#8211;<\/span> start<span class=\"token punctuation\">:<\/span><span class=\"token format-spec\">.2f<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\u79d2&#039;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> result<br \/>\n    <span class=\"token keyword\">return<\/span> wrapper<\/p>\n<p><span class=\"token comment\"># \u6d4b\u8bd5<\/span><br \/>\n<span class=\"token decorator annotation punctuation\">&#064;timer<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">slow_func<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    time<span class=\"token punctuation\">.<\/span>sleep<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u6a21\u62df\u8017\u65f6\u64cd\u4f5c<\/span><\/p>\n<p>slow_func<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8f93\u51fa&#xff1a;slow_func \u6267\u884c\u65f6\u95f4&#xff1a;1.00\u79d2<\/span><\/p>\n<h4>&#x1f9e9; \u7ec3\u4e602&#xff1a;\u5b9e\u73b0\u517c\u5bb9\u4e24\u79cd\u5f62\u5f0f\u7684\u88c5\u9970\u5668<\/h4>\n<p>\u9700\u6c42&#xff1a;\u7f16\u5199\u4e00\u4e2a log \u88c5\u9970\u5668&#xff0c;\u65e2\u80fd\u652f\u6301 &#064;log&#xff08;\u4e0d\u5e26\u53c2\u6570&#xff09;&#xff0c;\u53c8\u80fd\u652f\u6301 &#064;log(&#039;execute&#039;)&#xff08;\u5e26\u53c2\u6570&#xff09;\u3002<\/p>\n<p>\u5206\u6790&#xff1a;<\/p>\n<ul>\n<li>\u4e0d\u5e26\u53c2\u6570\u65f6&#xff0c;\u88c5\u9970\u5668\u76f4\u63a5\u63a5\u6536\u88ab\u88c5\u9970\u7684\u51fd\u6570&#xff08;\u53ef\u8c03\u7528\u5bf9\u8c61&#xff09;&#xff1b;<\/li>\n<li>\u5e26\u53c2\u6570\u65f6&#xff0c;\u88c5\u9970\u5668\u5148\u63a5\u6536\u53c2\u6570&#xff08;\u4e0d\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u5982\u5b57\u7b26\u4e32&#xff09;&#xff0c;\u518d\u8fd4\u56de\u5904\u7406\u51fd\u6570\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n<p>\u53ef\u901a\u8fc7 callable() \u51fd\u6570\u5224\u65ad\u53c2\u6570\u7c7b\u578b&#xff0c;\u533a\u5206\u4e24\u79cd\u573a\u666f&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> functools<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span>arg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u573a\u666f1&#xff1a;&#064;log \u5f62\u5f0f&#xff08;arg\u662f\u88ab\u88c5\u9970\u7684\u51fd\u6570&#xff09;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token builtin\">callable<\/span><span class=\"token punctuation\">(<\/span>arg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token decorator annotation punctuation\">&#064;functools<span class=\"token punctuation\">.<\/span>wraps<\/span><span class=\"token punctuation\">(<\/span>arg<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">def<\/span> <span class=\"token function\">wrapper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kwargs<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#039;Call <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>arg<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">()&#039;<\/span><\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u9ed8\u8ba4\u65e5\u5fd7<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> arg<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kwargs<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> wrapper<\/p>\n<p>    <span class=\"token comment\"># \u573a\u666f2&#xff1a;&#064;log(&#039;xxx&#039;) \u5f62\u5f0f&#xff08;arg\u662f\u88c5\u9970\u5668\u53c2\u6570&#xff09;<\/span><br \/>\n    <span class=\"token keyword\">else<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">def<\/span> <span class=\"token function\">decorator<\/span><span class=\"token punctuation\">(<\/span>fn<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token decorator annotation punctuation\">&#064;functools<span class=\"token punctuation\">.<\/span>wraps<\/span><span class=\"token punctuation\">(<\/span>fn<span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">def<\/span> <span class=\"token function\">wrapper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kwargs<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n                <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#039;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>arg<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\"> <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>fn<span class=\"token punctuation\">.<\/span>__name__<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">()&#039;<\/span><\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u81ea\u5b9a\u4e49\u65e5\u5fd7<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> fn<span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">**<\/span>kwargs<span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> wrapper<br \/>\n        <span class=\"token keyword\">return<\/span> decorator<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u6d4b\u8bd5\u4e0d\u5e26\u53c2\u6570<\/span><br \/>\n<span class=\"token decorator annotation punctuation\">&#064;log<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">f1<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6267\u884c f1&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\nf1<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u8f93\u51fa&#xff1a;Call f1() \\\\n \u6267\u884c f1<\/span><\/p>\n<p><span class=\"token comment\"># \u6d4b\u8bd5\u5e26\u53c2\u6570<\/span><br \/>\n<span class=\"token decorator annotation punctuation\">&#064;log<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;execute&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">f2<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6267\u884c f2&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\nf2<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u8f93\u51fa&#xff1a;execute f2() \\\\n \u6267\u884c f2<\/span><\/p>\n<p>\u539f\u7406&#xff1a;<\/p>\n<ul>\n<li>callable(arg) \u5224\u65ad arg \u662f\u5426\u4e3a\u53ef\u8c03\u7528\u5bf9\u8c61&#xff08;\u51fd\u6570&#xff09;&#xff1a;\n<ul>\n<li>\u662f \u2192 \u8fdb\u5165 if \u5206\u652f&#xff0c;\u76f4\u63a5\u5904\u7406\u88ab\u88c5\u9970\u51fd\u6570&#xff1b;<\/li>\n<li>\u5426 \u2192 \u8fdb\u5165 else \u5206\u652f&#xff0c;\u5148\u63a5\u6536\u53c2\u6570&#xff0c;\u518d\u8fd4\u56de\u5904\u7406\u51fd\u6570\u7684\u4e2d\u95f4\u903b\u8f91\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u4e03\u3001 \u603b\u7ed3<\/h3>\n<p>\u88c5\u9970\u5668\u662f Python \u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u7cbe\u9ad3&#xff0c;\u5b83\u8ba9\u4ee3\u7801\u66f4\u7075\u6d3b\u3001\u66f4\u6613\u7ef4\u62a4\u3002\u6838\u5fc3\u8981\u70b9&#xff1a;<\/p>\n<li>\u88c5\u9970\u5668\u901a\u8fc7\u5305\u88c5\u51fd\u6570\u5b9e\u73b0\u201c\u65e0\u4fb5\u5165\u5f0f\u589e\u5f3a\u201d&#xff1b;<\/li>\n<li>\u57fa\u7840\u88c5\u9970\u5668\u63a5\u6536\u51fd\u6570&#xff0c;\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\u9700\u8981\u4e09\u5c42\u5d4c\u5957&#xff1b;<\/li>\n<li>\u7528 functools.wraps \u4fdd\u7559\u539f\u51fd\u6570\u7684\u5143\u4fe1\u606f&#xff1b;<\/li>\n<li>\u9002\u7528\u4e8e\u65e5\u5fd7\u3001\u8ba1\u65f6\u3001\u6743\u9650\u9a8c\u8bc1\u7b49\u573a\u666f\u3002<\/li>\n<p>\u5408\u7406\u4f7f\u7528\u88c5\u9970\u5668&#xff0c;\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u7b80\u6d01\u3001\u66f4\u7075\u6d3b&#xff0c;\u540c\u65f6\u907f\u514d\u91cd\u590d\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb485\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf6\u6b21\u3002Python\u88c5\u9970\u5668\u662f\u4e00\u79cd\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u52a8\u6001\u589e\u5f3a\u51fd\u6570\u529f\u80fd\u7684\u6280\u672f\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u88c5\u9970\u5668\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406\uff1a\u88c5\u9970\u5668\u672c\u8d28\u662f\u8fd4\u56de\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u901a\u8fc7@\u8bed\u6cd5\u7cd6\u5b9e\u73b0\u51fd\u6570\u589e\u5f3a\u3002\u6587\u7ae0\u8be6\u7ec6\u8bb2\u89e3\u4e86\u57fa\u7840\u88c5\u9970\u5668\u3001\u5e26\u53c2\u6570\u88c5\u9970\u5668\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5f3a\u8c03\u4e86\u4f7f\u7528functools.wraps\u4fdd\u7559\u539f\u51fd\u6570\u5143\u4fe1\u606f\u7684\u91cd\u8981\u6027\uff0c\u5e76\u63d0\u4f9b\u4e86\u8ba1\u65f6\u88c5\u9970\u5668\u7684\u5b9e\u8df5\u6848\u4f8b\u3002\u88c5\u9970\u5668\u9002\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u3001\u6743\u9650\u9a8c\u8bc1\u3001\u6027\u80fd\u7edf\u8ba1\u7b49\u573a\u666f\uff0c\u80fd\u591f\u6709\u6548\u63d0\u5347\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,4754,190],"topic":[],"class_list":{"0":"post-49116","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-server","7":"tag-python","9":"tag-190"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/49116.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb485\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf6\u6b21\u3002Python\u88c5\u9970\u5668\u662f\u4e00\u79cd\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u52a8\u6001\u589e\u5f3a\u51fd\u6570\u529f\u80fd\u7684\u6280\u672f\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u88c5\u9970\u5668\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406\uff1a\u88c5\u9970\u5668\u672c\u8d28\u662f\u8fd4\u56de\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u901a\u8fc7@\u8bed\u6cd5\u7cd6\u5b9e\u73b0\u51fd\u6570\u589e\u5f3a\u3002\u6587\u7ae0\u8be6\u7ec6\u8bb2\u89e3\u4e86\u57fa\u7840\u88c5\u9970\u5668\u3001\u5e26\u53c2\u6570\u88c5\u9970\u5668\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5f3a\u8c03\u4e86\u4f7f\u7528functools.wraps\u4fdd\u7559\u539f\u51fd\u6570\u5143\u4fe1\u606f\u7684\u91cd\u8981\u6027\uff0c\u5e76\u63d0\u4f9b\u4e86\u8ba1\u65f6\u88c5\u9970\u5668\u7684\u5b9e\u8df5\u6848\u4f8b\u3002\u88c5\u9970\u5668\u9002\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u3001\u6743\u9650\u9a8c\u8bc1\u3001\u6027\u80fd\u7edf\u8ba1\u7b49\u573a\u666f\uff0c\u80fd\u591f\u6709\u6548\u63d0\u5347\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/49116.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T07:46:46+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49116.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/49116.html\",\"name\":\"1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T07:46:46+00:00\",\"dateModified\":\"2025-07-30T07:46:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/49116.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/49116.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49116.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/49116.html","og_locale":"zh_CN","og_type":"article","og_title":"1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb485\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf6\u6b21\u3002Python\u88c5\u9970\u5668\u662f\u4e00\u79cd\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u52a8\u6001\u589e\u5f3a\u51fd\u6570\u529f\u80fd\u7684\u6280\u672f\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u88c5\u9970\u5668\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406\uff1a\u88c5\u9970\u5668\u672c\u8d28\u662f\u8fd4\u56de\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u901a\u8fc7@\u8bed\u6cd5\u7cd6\u5b9e\u73b0\u51fd\u6570\u589e\u5f3a\u3002\u6587\u7ae0\u8be6\u7ec6\u8bb2\u89e3\u4e86\u57fa\u7840\u88c5\u9970\u5668\u3001\u5e26\u53c2\u6570\u88c5\u9970\u5668\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5f3a\u8c03\u4e86\u4f7f\u7528functools.wraps\u4fdd\u7559\u539f\u51fd\u6570\u5143\u4fe1\u606f\u7684\u91cd\u8981\u6027\uff0c\u5e76\u63d0\u4f9b\u4e86\u8ba1\u65f6\u88c5\u9970\u5668\u7684\u5b9e\u8df5\u6848\u4f8b\u3002\u88c5\u9970\u5668\u9002\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u3001\u6743\u9650\u9a8c\u8bc1\u3001\u6027\u80fd\u7edf\u8ba1\u7b49\u573a\u666f\uff0c\u80fd\u591f\u6709\u6548\u63d0\u5347\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/49116.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T07:46:46+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/49116.html","url":"https:\/\/www.wsisp.com\/helps\/49116.html","name":"1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T07:46:46+00:00","dateModified":"2025-07-30T07:46:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/49116.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/49116.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/49116.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"1\ufe0f\u20e32\ufe0f\u20e3 \u88c5\u9970\u5668\uff08Decorator\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=49116"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=49116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=49116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=49116"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=49116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}