{"id":300,"date":"2019-08-16T00:49:57","date_gmt":"2019-08-15T16:49:57","guid":{"rendered":"https:\/\/kanghaov.com\/?p=300"},"modified":"2024-11-26T15:46:45","modified_gmt":"2024-11-26T07:46:45","slug":"python%e8%a3%85%e9%a5%b0%e5%99%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/nemo.cool\/300.html","title":{"rendered":"Python\u88c5\u9970\u5668\u8be6\u89e3"},"content":{"rendered":"<p><a href=\"https:\/\/stackoverflow.com\/questions\/739654\/how-to-make-a-chain-of-function-decorators\/1594484#1594484\">How to make a chain of function decorators?<\/a><\/p>\n<h2>\u4ec0\u4e48\u662f\u88c5\u9970\u5668<\/h2>\n<ul>\n<li><code>\u88c5\u9970\u5668<\/code>\u7684\u672c\u8d28\u662f\u51fd\u6570\u3002<\/li>\n<li><code>\u88c5\u9970\u5668<\/code>\u653e\u5728\u4e00\u4e2a\u51fd\u6570\u5f00\u59cb\u5b9a\u4e49\u7684\u5730\u65b9\uff0c\u5c31\u50cf\u4e00\u9876\u5e3d\u5b50\u4e00\u6837\u6234\u5728\u8fd9\u4e2a\u51fd\u6570\u7684\u5934\u4e0a\u3002\u548c\u8fd9\u4e2a\u51fd\u6570\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002<\/li>\n<li>\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u7b2c\u4e00\u4ef6\u4e8b\u5e76\u4e0d\u662f\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u8fd9\u4e2a\u51fd\u6570\u505a\u4e3a\u53c2\u6570\u4f20\u5165\u5b83\u5934\u9876\u4e0a\u8fd9\u9876\u5e3d\u5b50\uff0c\u8fd9\u9876\u5e3d\u5b50\u6211\u4eec\u79f0\u4e4b\u4e3a<code>\u88c5\u9970\u5668<\/code>\u3002<\/li>\n<\/ul>\n<h2>\u4ece\u51fd\u6570\u5f00\u59cb<\/h2>\n<h3>\u51fd\u6570\u5b9a\u4e49<\/h3>\n<p>\u7406\u89e3\u88c5\u9970\u5668\u524d\uff0c\u9700\u8981\u660e\u767d\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u5148\u4ece\u4e00\u4e2a\u6700\u7b80\u5355\u51fd\u6570\u5b9a\u4e49\u5f00\u59cb\uff1a<\/p>\n<pre><code class=\"language-python \">def foo(num):\n    return num + 1\n<\/code><\/pre>\n<p>\u4e0a\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u540d\u5b57\u53ebfoo\uff0c\u4e5f\u53ef\u4ee5\u628a foo \u53ef\u7406\u89e3\u4e3a\u53d8\u91cf\u540d\uff0c\u8be5\u53d8\u91cf\u6307\u5411\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/15\/56b8ce8ededf5.png\" alt=\"v2-1cb13760d1d13840d0c28cb613238503_b.png\" \/><\/p>\n<p>\u8c03\u7528\u51fd\u6570\u53ea\u9700\u8981\u7ed9\u51fd\u6570\u540d\u52a0\u4e0a\u62ec\u53f7\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\uff08\u5982\u679c\u51fd\u6570\u5b9a\u4e49\u7684\u65f6\u5019\u6709\u53c2\u6570\u7684\u8bdd\uff09<\/p>\n<pre><code class=\"language-python \">value = foo(3)\nprint(value) # 4\n<\/code><\/pre>\n<p>\u53d8\u91cf\u540d foo \u73b0\u5728\u6307\u5411 <function foo at 0x1030060c8> \u51fd\u6570\u5bf9\u8c61\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u6307\u5411\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-python \">def bar():\n    print(\"bar\")\nfoo = bar\nfoo() # bar\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/15\/1ee2ed5262648.png\" alt=\"v2-b971edb7c66478a691ab96053482674f_b.png\" \/><\/p>\n<h3>\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c<\/h3>\n<p>\u5728Python\u4e2d\uff0c\u4e00\u5207\u7686\u4e3a\u5bf9\u8c61\uff0c\u51fd\u6570\u4e5f\u4e0d\u4f8b\u5916\uff0c\u5b83\u53ef\u4ee5\u50cf\u6574\u6570\u4e00\u6837\u4f5c\u4e3a\u5176\u5b83\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python \">def foo():\n    return 1\n\ndef bar():\n    return foo #\u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709\u62ec\u53f7\n\nprint(bar()) # &lt;function foo at 0x10a2f4140&gt;\n\nprint(bar()()) # 1\n# \u7b49\u4ef7\u4e8e\nprint(foo()) # 1\n<\/code><\/pre>\n<p>\u8c03\u7528\u51fd\u6570 bar() \u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61 \uff0c\u56e0\u4e3a\u8fd4\u56de\u503c\u662f\u51fd\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u8c03\u7528\uff08\u8bb0\u4f4f\uff1a<strong>\u8c03\u7528\u51fd\u6570\u5c31\u662f\u5728\u51fd\u6570\u540d\u540e\u9762\u52a0()<\/strong>\uff09\u8c03\u7528bar()()\u76f8\u5f53\u4e8e\u8c03\u7528 foo()\uff0c\u56e0\u4e3a \u53d8\u91cf foo \u6307\u5411\u7684\u5bf9\u8c61\u4e0e bar() \u7684\u8fd4\u56de\u503c\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/15\/09c4abd5d8918.png\" alt=\"v2-75a7a6c17eb23643beb0806a8466e0dd_b.png\" \/><\/p>\n<h3>\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/h3>\n<p>\u51fd\u6570\u8fd8\u53ef\u4ee5\u50cf\u6574\u6570\u4e00\u6837\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python \">def foo(num):\n    return num + 1\n\ndef bar(fun):\n    return fun(3)\n\nvalue = bar(foo)\nprint(value)  # 4\n<\/code><\/pre>\n<p>\u51fd\u6570 bar \u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5bf9\u8c61\uff0c\u628a\u51fd\u6570 foo \u4f20\u9012\u5230 bar\u4e2d\u53bb\u65f6\uff0cfoo \u548c fun \u4e24\u4e2a\u53d8\u91cf\u540d\u6307\u5411\u7684\u90fd\u662f\u540c\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u6240\u4ee5\u8c03\u7528 fun(3) \u76f8\u5f53\u4e8e\u8c03\u7528 foo(3)\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/15\/b554af1eb25a5.png\" alt=\"v2-552f0435dcc94697832b07ce921b5a10_b.png\" \/><\/p>\n<h3>\u51fd\u6570\u5d4c\u5957<\/h3>\n<p>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u51fd\u6570\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u4f5c\u4e3a\u5d4c\u5957\u51fd\u6570\u5b58\u5728\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python \">def outer():\n    x = 1\n    def inner():\n        print(x)\n    inner() # \u6ce8\u610f\u8fd9\u91cc\u6709\u62ec\u53f7\uff0c\u76f4\u63a5\u88ab\u8c03\u7528\n\nouter() # 1\n<\/code><\/pre>\n<p>inner\u505a\u4e3a\u5d4c\u5957\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u8c03\u7528 outer \u51fd\u6570\u65f6\uff0c\u53d1\u751f\u4e863\u4ef6\u4e8b\uff1a<\/p>\n<ol>\n<li>\u7ed9 \u53d8\u91cf x \u8d4b\u503c\u4e3a1<\/li>\n<li>\u5b9a\u4e49\u5d4c\u5957\u51fd\u6570 inner\uff0c<strong>\u6b64\u65f6\u5e76\u4e0d\u4f1a\u6267\u884c inner \u4e2d\u7684\u4ee3\u7801\uff0c\u56e0\u4e3a\u8be5\u51fd\u6570\u8fd8\u6ca1\u88ab\u8c03\u7528<\/strong>\uff0c\u76f4\u5230\u7b2c3\u6b65<\/li>\n<li>\u8c03\u7528 inner \u51fd\u6570\uff0c\u6267\u884c inner \u4e2d\u7684\u4ee3\u7801\u903b\u8f91\u3002<\/li>\n<\/ol>\n<h3>\u95ed\u5305<\/h3>\n<p>\u518d\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python \">def outer(x):\n    def inner():\n        print(x)\n\n    return inner #\u6ca1\u62ec\u53f7\uff0c\u4e0d\u88ab\u76f4\u63a5\u8c03\u7528\nclosure = outer(1) # closure\u5c31\u662f\u4e00\u4e2a\u95ed\u5305\nclosure() # 1\n<\/code><\/pre>\n<p>\u540c\u6837\u662f\u5d4c\u5957\u51fd\u6570\uff0c\u53ea\u662f\u7a0d\u6539\u52a8\u4e00\u4e0b\uff0c\u628a\u5c40\u90e8\u53d8\u91cf x \u4f5c\u4e3a\u53c2\u6570\u4e86\u4f20\u9012\u8fdb\u6765\uff0c<strong>\u5d4c\u5957\u51fd\u6570\u4e0d\u518d\u76f4\u63a5\u5728\u51fd\u6570\u91cc\u88ab\u8c03\u7528\uff0c\u800c\u662f\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de<\/strong>\uff0c\u8fd9\u91cc\u7684 closure\u5c31\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u672c\u8d28\u4e0a\u5b83\u8fd8\u662f\u51fd\u6570\uff0c<strong>\u95ed\u5305\u662f\u5f15\u7528\u4e86\u81ea\u7531\u53d8\u91cf(x)\u7684\u51fd\u6570(inner)<\/strong>\u3002<\/p>\n<h2>\u88c5\u9970\u5668\u57fa\u7840<\/h2>\n<p>\u7ee7\u7eed\u5f80\u4e0b\u770b\uff1a<\/p>\n<pre><code class=\"language-python \">def foo():\n    print(\"foo\")\n<\/code><\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u51fd\u6570\u8fd9\u53ef\u80fd\u662f\u53f2\u4e0a\u6700\u7b80\u5355\u7684\u4e1a\u52a1\u4ee3\u7801\u4e86\uff0c\u867d\u7136\u6ca1\u4ec0\u4e48\u7528\uff0c\u4f46\u662f\u80fd\u8bf4\u660e\u95ee\u9898\u5c31\u884c\u3002\u73b0\u5728\uff0c\u6709\u4e00\u4e2a\u65b0\u7684\u9700\u6c42\uff0c\u9700\u8981\u5728\u6267\u884c\u8be5\u51fd\u6570\u65f6\u52a0\u4e0a\u65e5\u5fd7\uff1a<\/p>\n<pre><code class=\"language-python \">def foo():\n    print(\"\u8bb0\u5f55\u65e5\u5fd7\u5f00\u59cb\")\n    print(\"foo\")\n    print(\"\u8bb0\u5f55\u65e5\u5fd7\u7ed3\u675f\")\n<\/code><\/pre>\n<p>\u529f\u80fd\u5b9e\u73b0\uff0c\u552f\u4e00\u7684\u95ee\u9898\u5c31\u662f\u5b83\u9700\u8981\u4fb5\u5165\u5230\u539f\u6765\u7684\u4ee3\u7801\u91cc\u9762\uff0c\u628a\u65e5\u5fd7\u903b\u8f91\u52a0\u4e0a\u53bb\uff0c\u5982\u679c\u8fd8\u6709\u597d\u51e0\u5341\u4e2a\u8fd9\u6837\u7684\u51fd\u6570\u8981\u52a0\u65e5\u5fd7\uff0c\u4e5f\u5fc5\u987b\u8fd9\u6837\u505a\uff0c\u663e\u7136\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u4e00\u70b9\u90fd\u4e0dPythonic\u3002\u90a3\u4e48\u6709\u6ca1\u6709\u53ef\u80fd<strong>\u5728\u4e0d\u4fee\u6539\u4e1a\u52a1\u4ee3\u7801\u7684\u524d\u63d0<\/strong>\u4e0b\uff0c\u5b9e\u73b0\u65e5\u5fd7\u529f\u80fd\u5462\uff1f\u7b54\u6848\u5c31\u662f\u88c5\u9970\u5668\u3002<\/p>\n<pre><code class=\"language-python \">def outer(func):\n    def inner():\n        print(\"\u8bb0\u5f55\u65e5\u5fd7\u5f00\u59cb\")\n        func() # \u4e1a\u52a1\u51fd\u6570\n        print(\"\u8bb0\u5f55\u65e5\u5fd7\u7ed3\u675f\")\n    return inner\n\ndef foo():\n    print(\"foo\")\n\nfoo = outer(foo)\nfoo()\n<\/code><\/pre>\n<p>\u6211\u6ca1\u6709\u4fee\u6539 foo \u51fd\u6570\u91cc\u9762\u7684\u4efb\u4f55\u903b\u8f91\uff0c\u53ea\u662f\u7ed9 foo \u53d8\u91cf\u91cd\u65b0\u8d4b\u503c\u4e86\uff0c\u6307\u5411\u4e86\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\u3002\u6700\u540e\u8c03\u7528 foo()\uff0c\u4e0d\u4ec5\u80fd\u6253\u5370\u65e5\u5fd7\uff0c\u4e1a\u52a1\u903b\u8f91\u4e5f\u6267\u884c\u5b8c\u4e86\u3002\u73b0\u5728\u6765\u5206\u6790\u4e00\u4e0b\u5b83\u7684\u6267\u884c\u6d41\u7a0b\u3002<\/p>\n<p>\u8fd9\u91cc\u7684 outer \u51fd\u6570\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u88c5\u9970\u5668\uff0c<strong>\u88c5\u9970\u5668\u662f\u4e00\u4e2a\u5e26\u6709\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\u7684\u95ed\u5305<\/strong>\uff0c<strong>\u672c\u8d28\u4e0a\u88c5\u9970\u5668\u4e5f\u662f\u51fd\u6570<\/strong>\u3002outer \u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f inner \u51fd\u6570\uff0c\u5728 inner \u51fd\u6570\u4e2d\uff0c\u9664\u4e86\u6267\u884c\u65e5\u5fd7\u64cd\u4f5c\uff0c\u8fd8\u6709\u4e1a\u52a1\u4ee3\u7801\uff0c\u8be5\u51fd\u6570\u91cd\u65b0\u8d4b\u503c\u7ed9 foo \u53d8\u91cf\u540e\uff0c\u8c03\u7528 foo() \u5c31\u76f8\u5f53\u4e8e\u8c03\u7528 inner()<\/p>\n<p>foo \u91cd\u65b0\u8d4b\u503c\u524d\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/15\/2150005c795c4.png\" alt=\"v2-7c9cc072ada43319cd30317b298ae660_b.png\" \/><\/p>\n<p>\u91cd\u65b0\u8d4b\u503c\u540e\uff0cfoo = outer(foo)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/15\/42f37636c4946.png\" alt=\"v2-d930c33f63f78ed8ef4b4bb0fb8c57ed_b.png\" \/><\/p>\n<p>\u53e6\u5916\uff0cPython\u4e3a\u88c5\u9970\u5668\u63d0\u4f9b\u4e86\u8bed\u6cd5\u7cd6 @\uff0c\u5b83\u7528\u5728\u51fd\u6570\u7684\u5b9a\u4e49\u5904\uff1a<\/p>\n<pre><code class=\"language-python \">@outer\ndef foo():\n    print(\"foo\")\n\nfoo()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u7701\u53bb\u4e86\u624b\u52a8\u7ed9foo\u91cd\u65b0\u8d4b\u503c\u7684\u6b65\u9aa4\u3002<\/p>\n<p>\u88c5\u9970\u5668\u7684\u4f7f\u7528\u65b9\u6cd5\u5f88\u56fa\u5b9a<\/p>\n<ol>\n<li>\u5148\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\uff08\u5e3d\u5b50\uff09<\/li>\n<li>\u518d\u5b9a\u4e49\u4f60\u7684\u4e1a\u52a1\u51fd\u6570\u6216\u8005\u7c7b\uff08\u4eba\uff09<\/li>\n<li>\u6700\u540e\u628a\u8fd9\u88c5\u9970\u5668\uff08\u5e3d\u5b50\uff09\u6263\u5728\u8fd9\u4e2a\u51fd\u6570\uff08\u4eba\uff09\u5934\u4e0a<\/li>\n<\/ol>\n<p>\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\u5b50<\/p>\n<pre><code class=\"language-python \">def decorator(func):\n    def wrapper(*args, **kw):\n        return func()\n    return wrapper\n\n@decorator\ndef function():\n    print(\"hello, decorator\")\n<\/code><\/pre>\n<p>\u5b9e\u9645\u4e0a\uff0c\u88c5\u9970\u5668\u5e76\u4e0d\u662f\u7f16\u7801\u5fc5\u987b\u6027\uff0c\u610f\u601d\u5c31\u662f\u8bf4\uff0c\u4f60\u4e0d\u4f7f\u7528\u88c5\u9970\u5668\u5b8c\u5168\u53ef\u4ee5\uff0c\u5b83\u7684\u51fa\u73b0\uff0c\u5e94\u8be5\u662f\u4f7f\u6211\u4eec\u7684\u4ee3\u7801<\/p>\n<ul>\n<li>\u66f4\u52a0\u4f18\u96c5\uff0c\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670<\/li>\n<li>\u5c06\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\u4ee3\u7801\u5c01\u88c5\u6210\u88c5\u9970\u5668\uff0c\u63d0\u9ad8\u4ee3\u7801\u590d\u7528\u7387\uff0c\u589e\u5f3a\u4ee3\u7801\u53ef\u8bfb\u6027<\/li>\n<\/ul>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u4ee5\u5b9e\u4f8b\u8bb2\u89e3\uff0c\u5982\u4f55\u7f16\u5199\u51fa\u5404\u79cd\u7b80\u5355\u53ca\u590d\u6742\u7684\u88c5\u9970\u5668\u3002<\/p>\n<h2>\u88c5\u9970\u5668\u8fdb\u9636<\/h2>\n<h3>\u65e5\u5fd7\u6253\u5370\u5668<\/h3>\n<p>\u9996\u5148\u662f<strong>\u65e5\u5fd7\u6253\u5370\u5668<\/strong> \u3002<br \/>\n\u5b9e\u73b0\u7684\u529f\u80fd\uff1a<\/p>\n<ol>\n<li>\u5728\u51fd\u6570\u6267\u884c\u524d\uff0c\u5148\u6253\u5370\u4e00\u884c\u65e5\u5fd7\u544a\u77e5\u4e00\u4e0b\u4e3b\u4eba\uff0c\u6211\u8981\u6267\u884c\u51fd\u6570\u4e86\u3002<\/li>\n<li>\u5728\u51fd\u6570\u6267\u884c\u5b8c\uff0c\u4e5f\u4e0d\u80fd\u62cd\u62cd\u5c41\u80a1\u5c31\u8d70\u4eba\u4e86\uff0c\u54b1\u53ef\u662f\u6709\u793c\u8c8c\u7684\u4ee3\u7801\uff0c\u518d\u6253\u5370\u4e00\u884c\u65e5\u5fd7\u544a\u77e5\u4e0b\u4e3b\u4eba\uff0c\u6211\u6267\u884c\u5b8c\u5566\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-python \"># \u8fd9\u662f\u88c5\u9970\u5668\u51fd\u6570\uff0c\u53c2\u6570 func \u662f\u88ab\u88c5\u9970\u7684\u51fd\u6570\ndef logger(func):\n    def wrapper(*args, **kw):\n        print('\u4e3b\u4eba\uff0c\u6211\u51c6\u5907\u5f00\u59cb\u6267\u884c\uff1a{} \u51fd\u6570\u4e86:'.format(func.__name__))\n\n        # \u771f\u6b63\u6267\u884c\u7684\u662f\u8fd9\u884c\u3002\n        func(*args, **kw)\n\n        print('\u4e3b\u4eba\uff0c\u6211\u6267\u884c\u5b8c\u5566\u3002')\n    return wrapper\n<\/code><\/pre>\n<p>\u5047\u5982\uff0c\u6211\u7684\u4e1a\u52a1\u51fd\u6570\u662f\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u4e4b\u548c\u3002\u5199\u597d\u540e\uff0c\u76f4\u63a5\u7ed9\u5b83\u5e26\u4e0a\u5e3d\u5b50\u3002<\/p>\n<pre><code class=\"language-python \">@logger\ndef add(x, y):\n    print('{} + {} = {}'.format(x, y, x+y))\n<\/code><\/pre>\n<p>\u7136\u540e\u6267\u884c\u4e00\u4e0b add \u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-python \">add(200, 50)\n<\/code><\/pre>\n<p>\u6765\u770b\u770b\u8f93\u51fa\u4e86\u4ec0\u4e48\uff1f<\/p>\n<pre><code class=\"language-python \">\u4e3b\u4eba\uff0c\u6211\u51c6\u5907\u5f00\u59cb\u6267\u884c\uff1aadd \u51fd\u6570\u4e86:\n200 + 50 = 250\n\u4e3b\u4eba\uff0c\u6211\u6267\u884c\u5b8c\u5566\u3002\n<\/code><\/pre>\n<h3>\u65f6\u95f4\u8ba1\u65f6\u5668<\/h3>\n<p>\u518d\u6765\u770b\u770b <strong>\u65f6\u95f4\u8ba1\u65f6\u5668<\/strong><br \/>\n\u5b9e\u73b0\u529f\u80fd\uff1a\u987e\u540d\u601d\u4e49\uff0c\u5c31\u662f\u8ba1\u7b97\u4e00\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u957f\u3002<\/p>\n<pre><code class=\"language-python \"># \u8fd9\u662f\u88c5\u9970\u51fd\u6570\ndef timer(func):\n    def wrapper(*args, **kw):\n        t1=time.time()\n        # \u8fd9\u662f\u51fd\u6570\u771f\u6b63\u6267\u884c\u7684\u5730\u65b9\n        func(*args, **kw)\n        t2=time.time()\n\n        # \u8ba1\u7b97\u4e0b\u65f6\u957f\n        cost_time = t2-t1\n        print(\"\u82b1\u8d39\u65f6\u95f4\uff1a{}\u79d2\".format(cost_time))\n    return wrapper\n<\/code><\/pre>\n<p>\u5047\u5982\uff0c\u6211\u4eec\u7684\u51fd\u6570\u662f\u8981\u7761\u772010\u79d2\u3002\u8fd9\u6837\u4e5f\u80fd\u66f4\u597d\u7684\u770b\u51fa\u8fd9\u4e2a\u8ba1\u7b97\u65f6\u957f\u5230\u5e95\u9760\u4e0d\u9760\u8c31\u3002<\/p>\n<pre><code class=\"language-python \">import time\n\n@timer\ndef want_sleep(sleep_time):\n    time.sleep(sleep_time)\n\nwant_sleep(10)\n<\/code><\/pre>\n<p>\u6765\u770b\u770b\u8f93\u51fa\uff0c\u5982\u9884\u671f\u4e00\u6837\uff0c\u8f93\u51fa10\u79d2\u3002<\/p>\n<pre><code class=\"language-python \">\u82b1\u8d39\u65f6\u95f4\uff1a10.0073800086975098\u79d2\n<\/code><\/pre>\n<h3>\u5e26\u53c2\u6570\u7684\u51fd\u6570\u88c5\u9970\u5668<\/h3>\n<p>\u901a\u8fc7\u4e0a\u9762\u4e24\u4e2a\u7b80\u5355\u7684\u5165\u95e8\u793a\u4f8b\uff0c\u4f60\u5e94\u8be5\u80fd\u4f53\u4f1a\u5230\u88c5\u9970\u5668\u7684\u5de5\u4f5c\u539f\u7406\u4e86\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u88c5\u9970\u5668\u7684\u7528\u6cd5\u8fd8\u8fdc\u4e0d\u6b62\u5982\u6b64\uff0c\u6df1\u7a76\u4e0b\u53bb\uff0c\u8fd8\u5927\u6709\u6587\u7ae0\u3002\u4eca\u5929\u5c31\u4e00\u8d77\u6765\u628a\u8fd9\u4e2a\u77e5\u8bc6\u70b9\u5b66\u900f\u3002<\/p>\n<p>\u56de\u8fc7\u5934\u53bb\u770b\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u88c5\u9970\u5668\u662f\u4e0d\u80fd\u63a5\u6536\u53c2\u6570\u7684\u3002\u5176\u7528\u6cd5\uff0c\u53ea\u80fd\u9002\u7528\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u573a\u666f\u3002\u4e0d\u4f20\u53c2\u7684\u88c5\u9970\u5668\uff0c\u53ea\u80fd\u5bf9\u88ab\u88c5\u9970\u51fd\u6570\uff0c\u6267\u884c\u56fa\u5b9a\u903b\u8f91\u3002<\/p>\n<p>\u88c5\u9970\u5668\u672c\u8eab\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u505a\u4e3a\u4e00\u4e2a\u51fd\u6570\uff0c\u5982\u679c\u4e0d\u80fd\u4f20\u53c2\uff0c\u90a3\u8fd9\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u5c31\u4f1a\u5f88\u53d7\u9650\uff0c\u53ea\u80fd\u6267\u884c\u56fa\u5b9a\u7684\u903b\u8f91\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u88c5\u9970\u5668\u7684\u903b\u8f91\u4ee3\u7801\u7684\u6267\u884c\u9700\u8981\u6839\u636e\u4e0d\u540c\u573a\u666f\u8fdb\u884c\u8c03\u6574\uff0c\u82e5\u4e0d\u80fd\u4f20\u53c2\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u8981\u5199\u4e24\u4e2a\u88c5\u9970\u5668\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5408\u7406\u7684\u3002<\/p>\n<p>\u6bd4\u5982\u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a\u53ef\u4ee5\u5b9a\u65f6\u53d1\u9001\u90ae\u4ef6\u7684\u4efb\u52a1\uff08\u4e00\u5206\u949f\u53d1\u9001\u4e00\u5c01\uff09\uff0c\u5b9a\u65f6\u8fdb\u884c\u65f6\u95f4\u540c\u6b65\u7684\u4efb\u52a1\uff08\u4e00\u5929\u540c\u6b65\u4e00\u6b21\uff09\uff0c\u5c31\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a periodic_task \uff08\u5b9a\u65f6\u4efb\u52a1\uff09\u7684\u88c5\u9970\u5668\uff0c\u8fd9\u4e2a\u88c5\u9970\u5668\u53ef\u4ee5\u63a5\u6536\u4e00\u4e2a\u65f6\u95f4\u95f4\u9694\u7684\u53c2\u6570\uff0c\u95f4\u9694\u591a\u957f\u65f6\u95f4\u6267\u884c\u4e00\u6b21\u4efb\u52a1\u3002<\/p>\n<p>\u53ef\u4ee5\u8fd9\u6837\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff0c\u7531\u4e8e\u8fd9\u4e2a\u529f\u80fd\u4ee3\u7801\u6bd4\u8f83\u590d\u6742\uff0c\u4e0d\u5229\u4e8e\u5b66\u4e60\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8d34\u4e86\u3002<\/p>\n<pre><code class=\"language-python \">@periodic_task(spacing=60)\ndef send_mail():\n     pass\n\n@periodic_task(spacing=86400)\ndef ntp()\n    pass\n<\/code><\/pre>\n<p>\u90a3\u6211\u4eec\u6765\u81ea\u5df1\u521b\u9020\u4e00\u4e2a\u4f2a\u573a\u666f\uff0c\u53ef\u4ee5\u5728\u88c5\u9970\u5668\u91cc\u4f20\u5165\u4e00\u4e2a\u53c2\u6570\uff0c\u6307\u660e\u56fd\u7c4d\uff0c\u5e76\u5728\u51fd\u6570\u6267\u884c\u524d\uff0c\u7528\u81ea\u5df1\u56fd\u5bb6\u7684\u6bcd\u8bed\u6253\u4e00\u4e2a\u62db\u547c\u3002<\/p>\n<pre><code class=\"language-python \"># \u5c0f\u660e\uff0c\u4e2d\u56fd\u4eba\n@say_hello(\"china\")\ndef xiaoming():\n    pass\n\n# jack\uff0c\u7f8e\u56fd\u4eba\n@say_hello(\"america\")\ndef jack():\n    pass\n<\/code><\/pre>\n<p>\u90a3\u6211\u4eec\u5982\u679c\u5b9e\u73b0\u8fd9\u4e2a\u88c5\u9970\u5668\uff0c\u8ba9\u5176\u53ef\u4ee5\u5b9e\u73b0<code>\u4f20\u53c2<\/code>\u5462\uff1f<\/p>\n<p>\u4f1a\u6bd4\u8f83\u590d\u6742\uff0c\u9700\u8981\u4e24\u5c42\u5d4c\u5957\u3002<\/p>\n<pre><code class=\"language-python \">def say_hello(country):\n    def wrapper(func):\n        def deco(*args, **kwargs):\n            if country == \"china\":\n                print(\"\u4f60\u597d!\")\n            elif country == \"america\":\n                print('hello.')\n            else:\n                return\n\n            # \u771f\u6b63\u6267\u884c\u51fd\u6570\u7684\u5730\u65b9\n            func(*args, **kwargs)\n        return deco\n    return wrapper\n<\/code><\/pre>\n<p>\u6765\u6267\u884c\u4e00\u4e0b<\/p>\n<pre><code class=\"language-python \">xiaoming()\nprint(\"------------\")\njack()\n<\/code><\/pre>\n<p>\u770b\u770b\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-python \">\u4f60\u597d!\n------------\nhello.\n<\/code><\/pre>\n<h3>\u9ad8\u9636\uff1a\u4e0d\u5e26\u53c2\u6570\u7684\u7c7b\u88c5\u9970\u5668<\/h3>\n<p>\u4ee5\u4e0a\u90fd\u662f\u57fa\u4e8e\u51fd\u6570\u5b9e\u73b0\u7684\u88c5\u9970\u5668\uff0c\u5728\u9605\u8bfb\u522b\u4eba\u4ee3\u7801\u65f6\uff0c\u8fd8\u53ef\u4ee5\u65f6\u5e38\u53d1\u73b0\u8fd8\u6709\u57fa\u4e8e\u7c7b\u5b9e\u73b0\u7684\u88c5\u9970\u5668\u3002<\/p>\n<p>\u57fa\u4e8e\u7c7b\u88c5\u9970\u5668\u7684\u5b9e\u73b0\uff0c\u5fc5\u987b\u5b9e\u73b0 <code>__call__<\/code> \u548c<code>__init__<\/code> \u4e24\u4e2a\u5185\u7f6e\u51fd\u6570\u3002<br \/>\n<code>__init__<\/code> \uff1a\u63a5\u6536\u88ab\u88c5\u9970\u51fd\u6570<br \/>\n<code>__call__<\/code>\uff1a\u5b9e\u73b0\u88c5\u9970\u903b\u8f91\u3002<\/p>\n<p>\u8fd8\u662f\u4ee5\u65e5\u5fd7\u6253\u5370\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4e3a\u4f8b<\/p>\n<pre><code class=\"language-python \">class logger(object):\n    def __init__(self, func):\n        self.func = func\n\n    def __call__(self, *args, **kwargs):\n        print(\"[INFO]: the function {func}() is running...\"\\\n            .format(func=self.func.__name__))\n        return self.func(*args, **kwargs)\n\n@logger\ndef say(something):\n    print(\"say {}!\".format(something))\n\nsay(\"hello\")\n<\/code><\/pre>\n<p>\u6267\u884c\u4e00\u4e0b\uff0c\u770b\u770b\u8f93\u51fa<\/p>\n<pre><code class=\"\">[INFO]: the function say() is running...\nsay hello!\n<\/code><\/pre>\n<h3>\u9ad8\u9636\uff1a\u5e26\u53c2\u6570\u7684\u7c7b\u88c5\u9970\u5668<\/h3>\n<p>\u4e0a\u9762\u4e0d\u5e26\u53c2\u6570\u7684\u4f8b\u5b50\uff0c\u4f60\u53d1\u73b0\u6ca1\u6709\uff0c\u53ea\u80fd\u6253\u5370 INFO \u7ea7\u522b\u7684\u65e5\u5fd7\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u6253\u5370<code>DEBUG<\/code> <code>WARNING<\/code> \u7b49\u7ea7\u522b\u7684\u65e5\u5fd7\u3002\u8fd9\u5c31\u9700\u8981\u7ed9\u7c7b\u88c5\u9970\u5668\u4f20\u5165\u53c2\u6570\uff0c\u7ed9\u8fd9\u4e2a\u51fd\u6570\u6307\u5b9a\u7ea7\u522b\u4e86\u3002<\/p>\n<p>\u5e26\u53c2\u6570\u548c\u4e0d\u5e26\u53c2\u6570\u7684\u7c7b\u88c5\u9970\u5668\u6709\u5f88\u5927\u7684\u4e0d\u540c\u3002<\/p>\n<p><code>__init__<\/code> \uff1a\u4e0d\u518d\u63a5\u6536\u88ab\u88c5\u9970\u51fd\u6570\uff0c\u800c\u662f\u63a5\u6536\u4f20\u5165\u53c2\u6570\u3002<br \/>\n<code>__call__<\/code>\uff1a\u63a5\u6536\u88ab\u88c5\u9970\u51fd\u6570\uff0c\u5b9e\u73b0\u88c5\u9970\u903b\u8f91\u3002<\/p>\n<pre><code class=\"language-python \">class logger(object):\n    def __init__(self, level='INFO'):\n        self.level = level\n\n    def __call__(self, func): # \u63a5\u53d7\u51fd\u6570\n        def wrapper(*args, **kwargs):\n            print(\"[{level}]: the function {func}() is running...\"\\\n                .format(level=self.level, func=func.__name__))\n            func(*args, **kwargs)\n        return wrapper  #\u8fd4\u56de\u51fd\u6570\n\n@logger(level='WARNING')\ndef say(something):\n    print(\"say {}!\".format(something))\n\nsay(\"hello\")\n<\/code><\/pre>\n<p>\u6211\u4eec\u6307\u5b9a WARNING \u7ea7\u522b\uff0c\u8fd0\u884c\u4e00\u4e0b\uff0c\u6765\u770b\u770b\u8f93\u51fa\u3002<\/p>\n<pre><code class=\"language-python \">[WARNING]: the function say() is running...\nsay hello!\n<\/code><\/pre>\n<h3>\u4f7f\u7528\u504f\u51fd\u6570\u4e0e\u7c7b\u5b9e\u73b0\u88c5\u9970\u5668<\/h3>\n<p>\u7edd\u5927\u591a\u6570\u88c5\u9970\u5668\u90fd\u662f\u57fa\u4e8e\u51fd\u6570\u548c\u95ed\u5305\u5b9e\u73b0\u7684\uff0c\u4f46\u8fd9\u5e76\u975e\u5236\u9020\u88c5\u9970\u5668\u7684\u552f\u4e00\u65b9\u5f0f\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0cPython \u5bf9\u67d0\u4e2a\u5bf9\u8c61\u662f\u5426\u80fd\u901a\u8fc7\u88c5\u9970\u5668\uff08<code>@decorator<\/code>\uff09\u5f62\u5f0f\u4f7f\u7528\u53ea\u6709\u4e00\u4e2a\u8981\u6c42\uff1a <strong>decorator \u5fc5\u987b\u662f\u4e00\u4e2a\u201c\u53ef\u88ab\u8c03\u7528\uff08callable\uff09\u7684\u5bf9\u8c61<\/strong> \u3002<\/p>\n<p>\u5bf9\u4e8e\u8fd9\u4e2a callable \u5bf9\u8c61\uff0c\u6211\u4eec\u6700\u719f\u6089\u7684\u5c31\u662f\u51fd\u6570\u4e86\u3002<\/p>\n<p>\u9664\u51fd\u6570\u4e4b\u5916\uff0c\u7c7b\u4e5f\u53ef\u4ee5\u662f callable \u5bf9\u8c61\uff0c\u53ea\u8981\u5b9e\u73b0\u4e86<code>__call__<\/code> \u51fd\u6570\uff08\u4e0a\u9762\u51e0\u4e2a\u4f8b\u5b50\u5df2\u7ecf\u63a5\u89e6\u8fc7\u4e86\uff09\u3002<\/p>\n<p>\u8fd8\u6709\u5bb9\u6613\u88ab\u4eba\u5ffd\u7565\u7684\u504f\u51fd\u6570\u5176\u5b9e\u4e5f\u662f callable \u5bf9\u8c61\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u5c31\u6765\u8bf4\u8bf4\uff0c\u5982\u4f55\u4f7f\u7528 \u7c7b\u548c\u504f\u51fd\u6570\u7ed3\u5408\u5b9e\u73b0\u4e00\u4e2a\u4e0e\u4f17\u4e0d\u540c\u7684\u88c5\u9970\u5668\u3002<\/p>\n<p>\u5982\u4e0b\u6240\u793a\uff0cDelayFunc \u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86<code>__call__<\/code> \u7684\u7c7b\uff0cdelay \u8fd4\u56de\u4e00\u4e2a\u504f\u51fd\u6570\uff0c\u5728\u8fd9\u91cc delay \u5c31\u53ef\u4ee5\u505a\u4e3a\u4e00\u4e2a\u88c5\u9970\u5668\u3002\uff08\u4ee5\u4e0b\u4ee3\u7801\u6458\u81ea Python\u5de5\u5320\uff1a\u4f7f\u7528\u88c5\u9970\u5668\u7684\u5c0f\u6280\u5de7\uff09<\/p>\n<pre><code class=\"language-python \">import time\nimport functools\n\nclass DelayFunc:\n    def __init__(self,  duration, func):\n        self.duration = duration\n        self.func = func\n\n    def __call__(self, *args, **kwargs):\n        print(f'Wait for {self.duration} seconds...')\n        time.sleep(self.duration)\n        return self.func(*args, **kwargs)\n\n    def eager_call(self, *args, **kwargs):\n        print('Call without delay')\n        return self.func(*args, **kwargs)\n\ndef delay(duration):\n    \"\"\"\n    \u88c5\u9970\u5668\uff1a\u63a8\u8fdf\u67d0\u4e2a\u51fd\u6570\u7684\u6267\u884c\u3002\n    \u540c\u65f6\u63d0\u4f9b .eager_call \u65b9\u6cd5\u7acb\u5373\u6267\u884c\n    \"\"\"\n    # \u6b64\u5904\u4e3a\u4e86\u907f\u514d\u5b9a\u4e49\u989d\u5916\u51fd\u6570\uff0c\n    # \u76f4\u63a5\u4f7f\u7528 functools.partial \u5e2e\u52a9\u6784\u9020 DelayFunc \u5b9e\u4f8b\n    return functools.partial(DelayFunc, duration)\n<\/code><\/pre>\n<p>\u6211\u4eec\u7684\u4e1a\u52a1\u51fd\u6570\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u76f8\u52a0<\/p>\n<pre><code class=\"language-python \">@delay(duration=2)\ndef add(a, b):\n    return a+b\n<\/code><\/pre>\n<p>\u6765\u770b\u4e00\u4e0b\u6267\u884c\u8fc7\u7a0b<\/p>\n<pre><code class=\"language-python \">&gt;&gt;&gt; add    # \u53ef\u89c1 add \u53d8\u6210\u4e86 Delay \u7684\u5b9e\u4f8b\n&lt;__main__.DelayFunc object at 0x107bd0be0&gt;\n&gt;&gt;&gt;\n&gt;&gt;&gt; add(3,5)  # \u76f4\u63a5\u8c03\u7528\u5b9e\u4f8b\uff0c\u8fdb\u5165 __call__\nWait for 2 seconds...\n8\n&gt;&gt;&gt;\n&gt;&gt;&gt; add.func # \u5b9e\u73b0\u5b9e\u4f8b\u65b9\u6cd5\n&lt;function add at 0x107bef1e0&gt;\n<\/code><\/pre>\n<h3>\u5982\u4f55\u5199\u80fd\u88c5\u9970\u7c7b\u7684\u88c5\u9970\u5668\uff1f<\/h3>\n<p>\u7528 Python \u5199\u5355\u4f8b\u6a21\u5f0f\u7684\u65f6\u5019\uff0c\u5e38\u7528\u7684\u6709\u4e09\u79cd\u5199\u6cd5\u3002\u5176\u4e2d\u4e00\u79cd\uff0c\u662f\u7528\u88c5\u9970\u5668\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u4ee5\u4e0b\u4fbf\u662f\u6211\u81ea\u5df1\u5199\u7684\u88c5\u9970\u5668\u7248\u7684\u5355\u4f8b\u5199\u6cd5\u3002<\/p>\n<pre><code class=\"language-python \">instances = {}\n\ndef singleton(cls):\n    def get_instance(*args, **kw):\n        cls_name = cls.__name__\n        print('===== 1 ====')\n        if not cls_name in instances:\n            print('===== 2 ====')\n            instance = cls(*args, **kw)\n            instances[cls_name] = instance\n        return instances[cls_name]\n    return get_instance\n\n@singleton\nclass User:\n    _instance = None\n\n    def __init__(self, name):\n        print('===== 3 ====')\n        self.name = name\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u7528singleton \u8fd9\u4e2a\u88c5\u9970\u51fd\u6570\u6765\u88c5\u9970 User \u8fd9\u4e2a\u7c7b\u3002\u88c5\u9970\u5668\u7528\u5728\u7c7b\u4e0a\uff0c\u5e76\u4e0d\u662f\u5f88\u5e38\u89c1\uff0c\u4f46\u53ea\u8981\u719f\u6089\u88c5\u9970\u5668\u7684\u5b9e\u73b0\u8fc7\u7a0b\uff0c\u5c31\u4e0d\u96be\u4ee5\u5b9e\u73b0\u5bf9\u7c7b\u7684\u88c5\u9970\u3002\u5728\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u88c5\u9970\u5668\u5c31\u53ea\u662f\u5b9e\u73b0\u5bf9\u7c7b\u5b9e\u4f8b\u7684\u751f\u6210\u7684\u63a7\u5236\u800c\u5df2\u3002<\/p>\n<p>\u5176\u5b9e\u4f8b\u5316\u7684\u8fc7\u7a0b\uff0c\u4f60\u53ef\u4ee5\u53c2\u8003\u6211\u8fd9\u91cc\u7684\u8c03\u8bd5\u8fc7\u7a0b\uff0c\u52a0\u4ee5\u7406\u89e3\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/15\/7bb8a514547b6.jpg\" alt=\"v2-be53ec93c67b08434aa4f1e129ff31f7_hd.jpg\" \/><\/p>\n<h3>wraps \u88c5\u9970\u5668\u6709\u5565\u7528\uff1f<\/h3>\n<p>\u5728 functools \u6807\u51c6\u5e93\u4e2d\u6709\u63d0\u4f9b\u4e00\u4e2a wraps \u88c5\u9970\u5668\uff0c\u4f60\u5e94\u8be5\u4e5f\u7ecf\u5e38\u89c1\u8fc7\uff0c\u90a3\u4ed6\u6709\u5565\u7528\u5462\uff1f<\/p>\n<p>\u5148\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<pre><code class=\"language-python \">def wrapper(func):\n    def inner_function():\n        pass\n    return inner_function\n\n@wrapper\ndef wrapped():\n    pass\n\nprint(wrapped.__name__)\n#inner_function\n<\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\u5b50\uff1f\u4e0d\u662f\u5e94\u8be5\u8fd4\u56de <code>func<\/code> \u5417\uff1f<\/p>\n<p>\u8fd9\u4e5f\u4e0d\u96be\u7406\u89e3\uff0c\u56e0\u4e3a\u4e0a\u8fb9\u6267\u884c <code>func<\/code>\u548c\u4e0b\u8fb9<code>decorator(func)<\/code>\u662f\u7b49\u4ef7\u7684\uff0c\u6240\u4ee5\u4e0a\u9762 <code>func.__name__<\/code> \u662f\u7b49\u4ef7\u4e8e\u4e0b\u9762 <code>decorator(func).__name__<\/code>\u7684\uff0c\u90a3\u5f53\u7136\u540d\u5b57\u662f <code>inner_function<\/code><\/p>\n<pre><code class=\"language-python \">def wrapper(func):\n    def inner_function():\n        pass\n    return inner_function\n\ndef wrapped():\n    pass\n\nprint(wrapper(wrapped).__name__)\n#inner_function\n<\/code><\/pre>\n<p>\u90a3\u5982\u4f55\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u4ea7\u751f\uff1f\u65b9\u6cd5\u662f\u4f7f\u7528 functools .wraps \u88c5\u9970\u5668\uff0c\u5b83\u7684\u4f5c\u7528\u5c31\u662f\u5c06 <strong>\u88ab\u4fee\u9970\u7684\u51fd\u6570(wrapped) **\u7684\u4e00\u4e9b\u5c5e\u6027\u503c\u8d4b\u503c\u7ed9 **\u4fee\u9970\u5668\u51fd\u6570(wrapper)<\/strong> \uff0c\u6700\u7ec8\u8ba9\u5c5e\u6027\u7684\u663e\u793a\u66f4\u7b26\u5408\u6211\u4eec\u7684\u76f4\u89c9\u3002<\/p>\n<pre><code class=\"language-python \">from functools import wraps\n\ndef wrapper(func):\n    @wraps(func)\n    def inner_function():\n        pass\n    return inner_function\n\n@wrapper\ndef wrapped():\n    pass\n\nprint(wrapped.__name__)\n# wrapped\n<\/code><\/pre>\n<p>\u51c6\u786e\u70b9\u8bf4\uff0cwraps \u5176\u5b9e\u662f\u4e00\u4e2a\u504f\u51fd\u6570\u5bf9\u8c61\uff08partial\uff09\uff0c\u6e90\u7801\u5982\u4e0b<\/p>\n<pre><code class=\"language-python \">def wraps(wrapped,\n          assigned = WRAPPER_ASSIGNMENTS,\n          updated = WRAPPER_UPDATES):\n    return partial(update_wrapper, wrapped=wrapped,\n                   assigned=assigned, updated=updated)\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230wraps\u5176\u5b9e\u5c31\u662f\u8c03\u7528\u4e86\u4e00\u4e2a\u51fd\u6570<code>update_wrapper<\/code>\uff0c\u77e5\u9053\u539f\u7406\u540e\uff0c\u6211\u4eec\u6539\u5199\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u5728\u4e0d\u4f7f\u7528 wraps\u7684\u60c5\u51b5\u4e0b\uff0c\u4e5f\u53ef\u4ee5\u8ba9<code>wrapped.__name__<\/code>\u6253\u5370\u51fa wrapped\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python \">from functools import update_wrapper\n\nWRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',\n                       '__annotations__')\n\ndef wrapper(func):\n    def inner_function():\n        pass\n\n    update_wrapper(inner_function, func, assigned=WRAPPER_ASSIGNMENTS)\n    return inner_function\n\n@wrapper\ndef wrapped():\n    pass\n\nprint(wrapped.__name__)\n<\/code><\/pre>\n<h3>\u5185\u7f6e\u88c5\u9970\u5668\uff1aproperty<\/h3>\n<p>\u4ee5\u4e0a\uff0c\u6211\u4eec\u4ecb\u7ecd\u7684\u90fd\u662f\u81ea\u5b9a\u4e49\u7684\u88c5\u9970\u5668\u3002<\/p>\n<p>\u5176\u5b9ePython\u8bed\u8a00\u672c\u8eab\u4e5f\u6709\u4e00\u4e9b\u88c5\u9970\u5668\u3002\u6bd4\u5982 property \u8fd9\u4e2a\u5185\u5efa\u88c5\u9970\u5668\uff0c\u6211\u4eec\u518d\u719f\u6089\u4e0d\u8fc7\u4e86\u3002<\/p>\n<p>\u5b83\u901a\u5e38\u5b58\u5728\u4e8e\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u6210\u4e00\u4e2a\u5c5e\u6027\uff0c\u5c5e\u6027\u7684\u503c\u5c31\u662f\u8be5\u51fd\u6570return\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u901a\u5e38\u6211\u4eec\u7ed9\u5b9e\u4f8b\u7ed1\u5b9a\u5c5e\u6027\u662f\u8fd9\u6837\u7684<\/p>\n<pre><code class=\"language-python \">class Student(object):\n    def __init__(self, name, age=None):\n        self.name = name\n        self.age = age\n\n# \u5b9e\u4f8b\u5316\nxiaoming = Student(\"\u5c0f\u660e\")\n\n# \u6dfb\u52a0\u5c5e\u6027\nxiaoming.age=25\n\n# \u67e5\u8be2\u5c5e\u6027\nxiaoming.age\n\n# \u5220\u9664\u5c5e\u6027\ndel xiaoming.age\n<\/code><\/pre>\n<p>\u4f46\u662f\u7a0d\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u4eba\u5458\uff0c\u4e00\u4e0b\u5c31\u53ef\u4ee5\u770b\u51fa\uff0c\u8fd9\u6837\u76f4\u63a5\u628a\u5c5e\u6027\u66b4\u9732\u51fa\u53bb\uff0c\u867d\u7136\u5199\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u4f46\u662f\u5e76\u4e0d\u80fd\u5bf9\u5c5e\u6027\u7684\u503c\u505a\u5408\u6cd5\u6027\u9650\u5236\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5199\u3002<\/p>\n<pre><code class=\"language-python \">class Student(object):\n    def __init__(self, name):\n        self.name = name\n        self.name = None\n\n    def set_age(self, age):\n        if not isinstance(age, int):\n            raise ValueError('\u8f93\u5165\u4e0d\u5408\u6cd5\uff1a\u5e74\u9f84\u5fc5\u987b\u4e3a\u6570\u503c!')\n        if not 0 &lt; age &lt; 100:\n            raise ValueError('\u8f93\u5165\u4e0d\u5408\u6cd5\uff1a\u5e74\u9f84\u8303\u56f4\u5fc5\u987b0-100')\n        self._age=age\n\n    def get_age(self):\n        return self._age\n\n    def del_age(self):\n        self._age = None\n\n\nxiaoming = Student(\"\u5c0f\u660e\")\n\n# \u6dfb\u52a0\u5c5e\u6027\nxiaoming.set_age(25)\n\n# \u67e5\u8be2\u5c5e\u6027\nxiaoming.get_age()\n\n# \u5220\u9664\u5c5e\u6027\nxiaoming.del_age()\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u8bbe\u8ba1\u867d\u7136\u53ef\u4ee5\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u4f46\u662f\u53ef\u4ee5\u53d1\u73b0\u4e0d\u7ba1\u662f\u83b7\u53d6\u8fd8\u662f\u8d4b\u503c\uff08\u901a\u8fc7\u51fd\u6570\uff09\u90fd\u548c\u6211\u4eec\u5e73\u65f6\u89c1\u5230\u7684\u4e0d\u4e00\u6837\u3002<br \/>\n\u6309\u7167\u6211\u4eec\u601d\u7ef4\u4e60\u60ef\u5e94\u8be5\u662f\u8fd9\u6837\u7684\u3002<\/p>\n<pre><code class=\"language-python \"># \u8d4b\u503c\nxiaoming.age = 25\n\n# \u83b7\u53d6\nxiaoming.age\n<\/code><\/pre>\n<p>\u90a3\u4e48\u8fd9\u6837\u7684\u65b9\u5f0f\u6211\u4eec\u5982\u4f55\u5b9e\u73b0\u5462\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<pre><code class=\"language-python \">class Student(object):\n    def __init__(self, name):\n        self.name = name\n        self.name = None\n\n    @property\n    def age(self):\n        return self._age\n\n    @age.setter\n    def age(self, value):\n        if not isinstance(value, int):\n            raise ValueError('\u8f93\u5165\u4e0d\u5408\u6cd5\uff1a\u5e74\u9f84\u5fc5\u987b\u4e3a\u6570\u503c!')\n        if not 0 &lt; value &lt; 100:\n            raise ValueError('\u8f93\u5165\u4e0d\u5408\u6cd5\uff1a\u5e74\u9f84\u8303\u56f4\u5fc5\u987b0-100')\n        self._age=value\n\n    @age.deleter\n    def age(self):\n        del self._age\n\nxiaoming = Student(\"\u5c0f\u660e\")\n\n# \u8bbe\u7f6e\u5c5e\u6027\nxiaoming.age = 25\n\n# \u67e5\u8be2\u5c5e\u6027\nxiaoming.age\n\n# \u5220\u9664\u5c5e\u6027\ndel xiaoming.age\n<\/code><\/pre>\n<p>\u7528 <code>@property<\/code> \u88c5\u9970\u8fc7\u7684\u51fd\u6570\uff0c\u4f1a\u5c06\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u6210\u4e00\u4e2a\u5c5e\u6027\uff0c\u5c5e\u6027\u7684\u503c\u5c31\u662f\u8be5\u51fd\u6570return\u7684\u5185\u5bb9\u3002\u540c\u65f6\uff0c\u4f1a\u5c06\u8fd9\u4e2a\u51fd\u6570\u53d8\u6210\u53e6\u5916\u4e00\u4e2a\u88c5\u9970\u5668\u3002\u5c31\u50cf\u540e\u9762\u6211\u4eec\u4f7f\u7528\u7684 <code>@age.setter<\/code> \u548c <code>@age.deleter<\/code> \u3002<\/p>\n<p><code>@age.setter<\/code> \u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>XiaoMing.age = 25<\/code>\u8fd9\u6837\u7684\u65b9\u5f0f\u76f4\u63a5\u8d4b\u503c\u3002<br \/>\n<code>@age.deleter<\/code> \u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>del XiaoMing.age<\/code> \u8fd9\u6837\u7684\u65b9\u5f0f\u6765\u5220\u9664\u5c5e\u6027\u3002<\/p>\n<p>property \u7684\u5e95\u5c42\u5b9e\u73b0\u673a\u5236\u662f\u300c\u63cf\u8ff0\u7b26\u300d\uff0c\u4e3a\u6b64\u6211\u8fd8\u5199\u8fc7\u4e00\u7bc7\u6587\u7ae0\u3002<\/p>\n<p>\u8fd9\u91cc\u4e5f\u4ecb\u7ecd\u4e00\u4e0b\u5427\uff0c\u6b63\u597d\u5c06\u8fd9\u4e9b\u770b\u4f3c\u96f6\u6563\u7684\u6587\u7ae0\u5168\u90e8\u4e32\u8d77\u6765\u3002<\/p>\n<p>\u5982\u4e0b\uff0c\u6211\u5199\u4e86\u4e00\u4e2a\u7c7b\uff0c\u91cc\u9762\u4f7f\u7528\u4e86 property \u5c06 math \u53d8\u6210\u4e86\u7c7b\u5b9e\u4f8b\u7684\u5c5e\u6027<\/p>\n<pre><code class=\"language-python \">class Student:\n    def __init__(self, name):\n        self.name = name\n\n    @property\n    def math(self):\n        return self._math\n\n    @math.setter\n    def math(self, value):\n        if 0 &lt;= value &lt;= 100:\n            self._math = value\n        else:\n            raise ValueError(\"Valid value must be in [0, 100]\")\n<\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u8bf4 property \u5e95\u5c42\u662f\u57fa\u4e8e\u63cf\u8ff0\u7b26\u534f\u8bae\u7684\u5462\uff1f\u901a\u8fc7 PyCharm \u70b9\u51fb\u8fdb\u5165 property \u7684\u6e90\u7801\uff0c\u5f88\u53ef\u60dc\uff0c\u53ea\u662f\u4e00\u4efd\u7c7b\u4f3c\u6587\u6863\u4e00\u6837\u7684\u4f2a\u6e90\u7801\uff0c\u5e76\u6ca1\u6709\u5176\u5177\u4f53\u7684\u5b9e\u73b0\u903b\u8f91\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u4ece\u8fd9\u4efd\u4f2a\u6e90\u7801\u7684\u9b54\u6cd5\u51fd\u6570\u7ed3\u6784\u7ec4\u6210\uff0c\u53ef\u4ee5\u5927\u4f53\u77e5\u9053\u5176\u5b9e\u73b0\u903b\u8f91\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u81ea\u5df1\u901a\u8fc7\u6a21\u4eff\u5176\u51fd\u6570\u7ed3\u6784\uff0c\u7ed3\u5408\u300c\u63cf\u8ff0\u7b26\u534f\u8bae\u300d\u6765\u81ea\u5df1\u5b9e\u73b0\u7c7b <code>property<\/code> \u7279\u6027\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python \">class TestProperty(object):\n\n    def __init__(self, fget=None, fset=None, fdel=None, doc=None):\n        self.fget = fget\n        self.fset = fset\n        self.fdel = fdel\n        self.__doc__ = doc\n\n    def __get__(self, obj, objtype=None):\n        print(\"in __get__\")\n        if obj is None:\n            return self\n        if self.fget is None:\n            raise AttributeError\n        return self.fget(obj)\n\n    def __set__(self, obj, value):\n        print(\"in __set__\")\n        if self.fset is None:\n            raise AttributeError\n        self.fset(obj, value)\n\n    def __delete__(self, obj):\n        print(\"in __delete__\")\n        if self.fdel is None:\n            raise AttributeError\n        self.fdel(obj)\n\n\n    def getter(self, fget):\n        print(\"in getter\")\n        return type(self)(fget, self.fset, self.fdel, self.__doc__)\n\n    def setter(self, fset):\n        print(\"in setter\")\n        return type(self)(self.fget, fset, self.fdel, self.__doc__)\n\n    def deleter(self, fdel):\n        print(\"in deleter\")\n        return type(self)(self.fget, self.fset, fdel, self.__doc__)\n<\/code><\/pre>\n<p>\u7136\u540e Student \u7c7b\uff0c\u6211\u4eec\u4e5f\u76f8\u5e94\u6539\u6210\u5982\u4e0b<\/p>\n<pre><code class=\"language-python \">class Student:\n    def __init__(self, name):\n        self.name = name\n\n    # \u5176\u5b9e\u53ea\u6709\u8fd9\u91cc\u6539\u53d8\n    @TestProperty\n    def math(self):\n        return self._math\n\n    @math.setter\n    def math(self, value):\n        if 0 &lt;= value &lt;= 100:\n            self._math = value\n        else:\n            raise ValueError(\"Valid value must be in [0, 100]\")\n<\/code><\/pre>\n<p>\u4e3a\u4e86\u5c3d\u91cf\u8ba9\u4f60\u5c11\u4ea7\u751f\u4e00\u70b9\u7591\u60d1\uff0c\u6211\u8fd9\u91cc\u505a\u4e24\u70b9\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528<code>TestProperty<\/code>\u88c5\u9970\u540e\uff0c<code>math<\/code> \u4e0d\u518d\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u662f<code>TestProperty<\/code>\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u6240\u4ee5\u7b2c\u4e8c\u4e2amath\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528 <code>math.setter<\/code> \u6765\u88c5\u9970\uff0c\u672c\u8d28\u662f\u8c03\u7528<code>TestProperty.setter<\/code> \u6765\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684 <code>TestProperty<\/code> \u5b9e\u4f8b\u8d4b\u503c\u7ed9\u7b2c\u4e8c\u4e2a<code>math<\/code>\u3002<\/li>\n<li>\u7b2c\u4e00\u4e2a <code>math<\/code> \u548c\u7b2c\u4e8c\u4e2a <code>math<\/code> \u662f\u4e24\u4e2a\u4e0d\u540c <code>TestProperty<\/code> \u5b9e\u4f8b\u3002\u4f46\u4ed6\u4eec\u90fd\u5c5e\u4e8e\u540c\u4e00\u4e2a\u63cf\u8ff0\u7b26\u7c7b\uff08TestProperty\uff09\uff0c\u5f53\u5bf9 math \u5bf9\u4e8e\u8d4b\u503c\u65f6\uff0c\u5c31\u4f1a\u8fdb\u5165 <code>TestProperty.__set__<\/code>\uff0c\u5f53\u5bf9math \u8fdb\u884c\u53d6\u503c\u91cc\uff0c\u5c31\u4f1a\u8fdb\u5165 <code>TestProperty.__get__<\/code>\u3002\u4ed4\u7ec6\u4e00\u770b\uff0c\u5176\u5b9e\u6700\u7ec8\u8bbf\u95ee\u7684\u8fd8\u662fStudent\u5b9e\u4f8b\u7684 <code>_math<\/code> \u5c5e\u6027\u3002<\/li>\n<\/ol>\n<p>\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u8fd8\u662f\u8fd0\u884c\u4e00\u4e0b\uff0c\u66f4\u52a0\u76f4\u89c2\u4e00\u70b9\u3002<\/p>\n<pre><code class=\"language-python \"># \u8fd0\u884c\u540e\uff0c\u4f1a\u76f4\u63a5\u6253\u5370\u8fd9\u4e00\u884c\uff0c\u8fd9\u662f\u5728\u5b9e\u4f8b\u5316 TestProperty \u5e76\u8d4b\u503c\u7ed9\u7b2c\u4e8c\u4e2amath\nin setter\n&gt;&gt;&gt;\n&gt;&gt;&gt; s1.math = 90\nin __set__\n&gt;&gt;&gt; s1.math\nin __get__\n90\n<\/code><\/pre>\n<p>\u5982\u5bf9\u4e0a\u9762\u4ee3\u7801\u7684\u8fd0\u884c\u539f\u7406\uff0c\u6709\u7591\u95ee\u7684\u540c\u5b66\uff0c\u8bf7\u52a1\u5fc5\u7ed3\u5408\u4e0a\u9762\u4e24\u70b9\u8bf4\u660e\u52a0\u4ee5\u7406\u89e3\uff0c\u90a3\u4e24\u70b9\u76f8\u5f53\u5173\u952e\u3002<\/p>\n<h3>\u5176\u4ed6\u88c5\u9970\u5668\uff1a\u88c5\u9970\u5668\u5b9e\u6218<\/h3>\n<p>\u8bfb\u5b8c\u5e76\u7406\u89e3\u4e86\u4e0a\u9762\u7684\u5185\u5bb9\uff0c\u4f60\u53ef\u4ee5\u8bf4\u662fPython\u9ad8\u624b\u4e86\u3002\u522b\u6000\u7591\uff0c\u81ea\u4fe1\u70b9\uff0c\u56e0\u4e3a\u5f88\u591a\u4eba\u90fd\u4e0d\u77e5\u9053\u88c5\u9970\u5668\u6709\u8fd9\u4e48\u591a\u7528\u6cd5\u5462\u3002<\/p>\n<p>\u5728\u6211\u770b\u6765\uff0c\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u8fbe\u5230\u5982\u4e0b\u76ee\u7684\uff1a<\/p>\n<ul>\n<li>\u4f7f\u4ee3\u7801\u53ef\u8bfb\u6027\u66f4\u9ad8\uff0c\u903c\u683c\u66f4\u9ad8\uff1b<\/li>\n<li>\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\uff0c\u4ee3\u7801\u5197\u4f59\u5ea6\u66f4\u4f4e\uff1b<\/li>\n<\/ul>\n<p>\u521a\u597d\u6211\u5728\u6700\u8fd1\u4e5f\u6709\u4e00\u4e2a\u573a\u666f\uff0c\u53ef\u4ee5\u7528\u88c5\u9970\u5668\u5f88\u597d\u7684\u5b9e\u73b0\uff0c\u6682\u4e14\u653e\u4e0a\u6765\u770b\u770b\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5b9e\u73b0\u63a7\u5236\u51fd\u6570\u8fd0\u884c\u8d85\u65f6\u7684\u88c5\u9970\u5668\u3002\u5982\u679c\u8d85\u65f6\uff0c\u5219\u4f1a\u629b\u51fa\u8d85\u65f6\u5f02\u5e38\u3002<\/p>\n<p>\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u770b\u770b\u3002<\/p>\n<pre><code class=\"language-python \">import signal\n\nclass TimeoutException(Exception):\n    def __init__(self, error='Timeout waiting for response from Cloud'):\n        Exception.__init__(self, error)\n\n\ndef timeout_limit(timeout_time):\n    def wraps(func):\n        def handler(signum, frame):\n            raise TimeoutException()\n\n        def deco(*args, **kwargs):\n            signal.signal(signal.SIGALRM, handler)\n            signal.alarm(timeout_time)\n            func(*args, **kwargs)\n            signal.alarm(0)\n        return deco\n    return wraps\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to make a chain of function decorators? \u4ec0\u4e48\u662f\u88c5\u9970\u5668 \u88c5\u9970\u5668\u7684\u672c\u8d28\u662f\u51fd\u6570\u3002 \u88c5\u9970\u5668\u653e\u5728\u4e00\u4e2a\u51fd\u6570\u5f00\u59cb\u5b9a\u4e49\u7684\u5730\u65b9\uff0c\u5c31\u50cf\u4e00\u9876\u5e3d\u5b50\u4e00\u6837\u6234\u5728\u8fd9\u4e2a\u51fd\u6570\u7684\u5934\u4e0a\u3002\u548c\u8fd9\u4e2a\u51fd\u6570\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u7b2c\u4e00\u4ef6\u4e8b\u5e76\u4e0d\u662f\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u8fd9\u4e2a\u51fd\u6570\u505a\u4e3a\u53c2\u6570\u4f20\u5165\u5b83\u5934\u9876\u4e0a\u8fd9\u9876\u5e3d\u5b50\uff0c\u8fd9\u9876\u5e3d\u5b50\u6211\u4eec\u79f0\u4e4b\u4e3a\u88c5\u9970\u5668\u3002 \u4ece\u51fd\u6570\u5f00\u59cb \u51fd\u6570\u5b9a\u4e49 \u7406\u89e3\u88c5\u9970\u5668\u524d\uff0c\u9700\u8981\u660e\u767d\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u5148\u4ece\u4e00\u4e2a\u6700\u7b80\u5355\u51fd\u6570\u5b9a\u4e49\u5f00\u59cb\uff1a def foo(num): return num + 1 \u4e0a\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u540d\u5b57\u53ebfoo\uff0c\u4e5f\u53ef\u4ee5\u628a foo \u53ef\u7406\u89e3\u4e3a\u53d8\u91cf\u540d\uff0c\u8be5\u53d8\u91cf\u6307\u5411\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61: \u8c03\u7528\u51fd\u6570\u53ea\u9700\u8981\u7ed9\u51fd\u6570\u540d\u52a0\u4e0a\u62ec\u53f7\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\uff08\u5982\u679c\u51fd\u6570\u5b9a\u4e49\u7684\u65f6\u5019\u6709\u53c2\u6570\u7684\u8bdd\uff09 value = foo(3) print(value) # 4 \u53d8\u91cf\u540d foo \u73b0\u5728\u6307\u5411 \u51fd\u6570\u5bf9\u8c61\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u6307\u5411\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002 def bar(): print(&#8220;bar&#8221;) foo = bar foo() # bar \u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c \u5728Python\u4e2d\uff0c\u4e00\u5207\u7686\u4e3a\u5bf9\u8c61\uff0c\u51fd\u6570\u4e5f\u4e0d\u4f8b\u5916\uff0c\u5b83\u53ef\u4ee5\u50cf\u6574\u6570\u4e00\u6837\u4f5c\u4e3a\u5176\u5b83\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f8b\u5982\uff1a def foo(): return 1 def bar(): return foo #\u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709\u62ec\u53f7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":301,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86,2],"tags":[10,47,46],"class_list":["post-300","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","category-py","tag-python","tag-47","tag-46"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python\u88c5\u9970\u5668\u8be6\u89e3 - Nemo<\/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:\/\/nemo.cool\/300.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u88c5\u9970\u5668\u8be6\u89e3 - Nemo\" \/>\n<meta property=\"og:description\" content=\"How to make a chain of function decorators? \u4ec0\u4e48\u662f\u88c5\u9970\u5668 \u88c5\u9970\u5668\u7684\u672c\u8d28\u662f\u51fd\u6570\u3002 \u88c5\u9970\u5668\u653e\u5728\u4e00\u4e2a\u51fd\u6570\u5f00\u59cb\u5b9a\u4e49\u7684\u5730\u65b9\uff0c\u5c31\u50cf\u4e00\u9876\u5e3d\u5b50\u4e00\u6837\u6234\u5728\u8fd9\u4e2a\u51fd\u6570\u7684\u5934\u4e0a\u3002\u548c\u8fd9\u4e2a\u51fd\u6570\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u7b2c\u4e00\u4ef6\u4e8b\u5e76\u4e0d\u662f\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u8fd9\u4e2a\u51fd\u6570\u505a\u4e3a\u53c2\u6570\u4f20\u5165\u5b83\u5934\u9876\u4e0a\u8fd9\u9876\u5e3d\u5b50\uff0c\u8fd9\u9876\u5e3d\u5b50\u6211\u4eec\u79f0\u4e4b\u4e3a\u88c5\u9970\u5668\u3002 \u4ece\u51fd\u6570\u5f00\u59cb \u51fd\u6570\u5b9a\u4e49 \u7406\u89e3\u88c5\u9970\u5668\u524d\uff0c\u9700\u8981\u660e\u767d\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u5148\u4ece\u4e00\u4e2a\u6700\u7b80\u5355\u51fd\u6570\u5b9a\u4e49\u5f00\u59cb\uff1a def foo(num): return num + 1 \u4e0a\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u540d\u5b57\u53ebfoo\uff0c\u4e5f\u53ef\u4ee5\u628a foo \u53ef\u7406\u89e3\u4e3a\u53d8\u91cf\u540d\uff0c\u8be5\u53d8\u91cf\u6307\u5411\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61: \u8c03\u7528\u51fd\u6570\u53ea\u9700\u8981\u7ed9\u51fd\u6570\u540d\u52a0\u4e0a\u62ec\u53f7\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\uff08\u5982\u679c\u51fd\u6570\u5b9a\u4e49\u7684\u65f6\u5019\u6709\u53c2\u6570\u7684\u8bdd\uff09 value = foo(3) print(value) # 4 \u53d8\u91cf\u540d foo \u73b0\u5728\u6307\u5411 \u51fd\u6570\u5bf9\u8c61\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u6307\u5411\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002 def bar(): print(&quot;bar&quot;) foo = bar foo() # bar \u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c \u5728Python\u4e2d\uff0c\u4e00\u5207\u7686\u4e3a\u5bf9\u8c61\uff0c\u51fd\u6570\u4e5f\u4e0d\u4f8b\u5916\uff0c\u5b83\u53ef\u4ee5\u50cf\u6574\u6570\u4e00\u6837\u4f5c\u4e3a\u5176\u5b83\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f8b\u5982\uff1a def foo(): return 1 def bar(): return foo #\u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709\u62ec\u53f7 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nemo.cool\/300.html\" \/>\n<meta property=\"og:site_name\" content=\"Nemo\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-15T16:49:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-26T07:46:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/decorator_840x340.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"840\" \/>\n\t<meta property=\"og:image:height\" content=\"340\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Nemo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nemo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html\"},\"author\":{\"name\":\"Nemo\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"headline\":\"Python\u88c5\u9970\u5668\u8be6\u89e3\",\"datePublished\":\"2019-08-15T16:49:57+00:00\",\"dateModified\":\"2024-11-26T07:46:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html\"},\"wordCount\":101,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"image\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/decorator_840x340.jpg\",\"keywords\":[\"python\",\"\u88c5\u9970\u5668\",\"\u8bed\u6cd5\"],\"articleSection\":[\"Dev\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nemo.cool\\\/300.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html\",\"url\":\"https:\\\/\\\/nemo.cool\\\/300.html\",\"name\":\"Python\u88c5\u9970\u5668\u8be6\u89e3 - Nemo\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/decorator_840x340.jpg\",\"datePublished\":\"2019-08-15T16:49:57+00:00\",\"dateModified\":\"2024-11-26T07:46:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nemo.cool\\\/300.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html#primaryimage\",\"url\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/decorator_840x340.jpg\",\"contentUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/decorator_840x340.jpg\",\"width\":840,\"height\":340},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/300.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/nemo.cool\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u88c5\u9970\u5668\u8be6\u89e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/#website\",\"url\":\"https:\\\/\\\/nemo.cool\\\/\",\"name\":\"Nemo\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nemo.cool\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\",\"name\":\"Nemo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"url\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"caption\":\"Nemo\"},\"logo\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\"},\"sameAs\":[\"https:\\\/\\\/nemo.cool\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u88c5\u9970\u5668\u8be6\u89e3 - Nemo","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:\/\/nemo.cool\/300.html","og_locale":"en_US","og_type":"article","og_title":"Python\u88c5\u9970\u5668\u8be6\u89e3 - Nemo","og_description":"How to make a chain of function decorators? \u4ec0\u4e48\u662f\u88c5\u9970\u5668 \u88c5\u9970\u5668\u7684\u672c\u8d28\u662f\u51fd\u6570\u3002 \u88c5\u9970\u5668\u653e\u5728\u4e00\u4e2a\u51fd\u6570\u5f00\u59cb\u5b9a\u4e49\u7684\u5730\u65b9\uff0c\u5c31\u50cf\u4e00\u9876\u5e3d\u5b50\u4e00\u6837\u6234\u5728\u8fd9\u4e2a\u51fd\u6570\u7684\u5934\u4e0a\u3002\u548c\u8fd9\u4e2a\u51fd\u6570\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002 \u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u7b2c\u4e00\u4ef6\u4e8b\u5e76\u4e0d\u662f\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u8fd9\u4e2a\u51fd\u6570\u505a\u4e3a\u53c2\u6570\u4f20\u5165\u5b83\u5934\u9876\u4e0a\u8fd9\u9876\u5e3d\u5b50\uff0c\u8fd9\u9876\u5e3d\u5b50\u6211\u4eec\u79f0\u4e4b\u4e3a\u88c5\u9970\u5668\u3002 \u4ece\u51fd\u6570\u5f00\u59cb \u51fd\u6570\u5b9a\u4e49 \u7406\u89e3\u88c5\u9970\u5668\u524d\uff0c\u9700\u8981\u660e\u767d\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u5148\u4ece\u4e00\u4e2a\u6700\u7b80\u5355\u51fd\u6570\u5b9a\u4e49\u5f00\u59cb\uff1a def foo(num): return num + 1 \u4e0a\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u540d\u5b57\u53ebfoo\uff0c\u4e5f\u53ef\u4ee5\u628a foo \u53ef\u7406\u89e3\u4e3a\u53d8\u91cf\u540d\uff0c\u8be5\u53d8\u91cf\u6307\u5411\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61: \u8c03\u7528\u51fd\u6570\u53ea\u9700\u8981\u7ed9\u51fd\u6570\u540d\u52a0\u4e0a\u62ec\u53f7\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\uff08\u5982\u679c\u51fd\u6570\u5b9a\u4e49\u7684\u65f6\u5019\u6709\u53c2\u6570\u7684\u8bdd\uff09 value = foo(3) print(value) # 4 \u53d8\u91cf\u540d foo \u73b0\u5728\u6307\u5411 \u51fd\u6570\u5bf9\u8c61\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u6307\u5411\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u3002 def bar(): print(\"bar\") foo = bar foo() # bar \u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c \u5728Python\u4e2d\uff0c\u4e00\u5207\u7686\u4e3a\u5bf9\u8c61\uff0c\u51fd\u6570\u4e5f\u4e0d\u4f8b\u5916\uff0c\u5b83\u53ef\u4ee5\u50cf\u6574\u6570\u4e00\u6837\u4f5c\u4e3a\u5176\u5b83\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u4f8b\u5982\uff1a def foo(): return 1 def bar(): return foo #\u6ce8\u610f\u8fd9\u91cc\u6ca1\u6709\u62ec\u53f7 [&hellip;]","og_url":"https:\/\/nemo.cool\/300.html","og_site_name":"Nemo","article_published_time":"2019-08-15T16:49:57+00:00","article_modified_time":"2024-11-26T07:46:45+00:00","og_image":[{"width":840,"height":340,"url":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/decorator_840x340.jpg","type":"image\/jpeg"}],"author":"Nemo","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nemo","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nemo.cool\/300.html#article","isPartOf":{"@id":"https:\/\/nemo.cool\/300.html"},"author":{"name":"Nemo","@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"headline":"Python\u88c5\u9970\u5668\u8be6\u89e3","datePublished":"2019-08-15T16:49:57+00:00","dateModified":"2024-11-26T07:46:45+00:00","mainEntityOfPage":{"@id":"https:\/\/nemo.cool\/300.html"},"wordCount":101,"commentCount":0,"publisher":{"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"image":{"@id":"https:\/\/nemo.cool\/300.html#primaryimage"},"thumbnailUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/decorator_840x340.jpg","keywords":["python","\u88c5\u9970\u5668","\u8bed\u6cd5"],"articleSection":["Dev","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nemo.cool\/300.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nemo.cool\/300.html","url":"https:\/\/nemo.cool\/300.html","name":"Python\u88c5\u9970\u5668\u8be6\u89e3 - Nemo","isPartOf":{"@id":"https:\/\/nemo.cool\/#website"},"primaryImageOfPage":{"@id":"https:\/\/nemo.cool\/300.html#primaryimage"},"image":{"@id":"https:\/\/nemo.cool\/300.html#primaryimage"},"thumbnailUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/decorator_840x340.jpg","datePublished":"2019-08-15T16:49:57+00:00","dateModified":"2024-11-26T07:46:45+00:00","breadcrumb":{"@id":"https:\/\/nemo.cool\/300.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nemo.cool\/300.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nemo.cool\/300.html#primaryimage","url":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/decorator_840x340.jpg","contentUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/decorator_840x340.jpg","width":840,"height":340},{"@type":"BreadcrumbList","@id":"https:\/\/nemo.cool\/300.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/nemo.cool\/"},{"@type":"ListItem","position":2,"name":"Python\u88c5\u9970\u5668\u8be6\u89e3"}]},{"@type":"WebSite","@id":"https:\/\/nemo.cool\/#website","url":"https:\/\/nemo.cool\/","name":"Nemo","description":"","publisher":{"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nemo.cool\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2","name":"Nemo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","url":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","contentUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","caption":"Nemo"},"logo":{"@id":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg"},"sameAs":["https:\/\/nemo.cool"]}]}},"_links":{"self":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/comments?post=300"}],"version-history":[{"count":0,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts\/300\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/media\/301"}],"wp:attachment":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/categories?post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/tags?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}