{"id":6294,"date":"2017-08-23T11:28:50","date_gmt":"2017-08-23T03:28:50","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6294"},"modified":"2017-09-20T10:40:58","modified_gmt":"2017-09-20T02:40:58","slug":"python-%e8%a3%85%e9%a5%b0%e5%99%a8%e4%bd%bf%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6294.html","title":{"rendered":"Python\u88c5\u9970\u5668\u4f7f\u7528\u6307\u5357"},"content":{"rendered":"<h1>\u88c5\u9970\u5668\u57fa\u7840\u77e5\u8bc6<\/h1>\n<p>\u9996\u5148\u770b\u4e00\u4e0b\u8fd9\u6bb5\u4ee3\u7801<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef deco(fn):\r\n    print &quot;I am %s!&quot; % fn.__name__\r\n\r\n@deco\r\ndef func():\r\n    pass\r\n\r\n# output\r\nI am func\r\n\r\n# \u6ca1\u6709\u6267\u884cfunc \u51fd\u6570 \u4f46\u662f deco \u88ab\u6267\u884c\u4e86\r\n<\/pre>\n<p>\u5728\u7528\u67d0\u4e2a@decorator\u6765\u4fee\u9970\u67d0\u4e2a\u51fd\u6570func\u65f6<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@decorator\r\ndef func():\r\n    pass\r\n<\/pre>\n<p>\u5176\u89e3\u91ca\u5668\u4f1a\u89e3\u91ca\u6210\u4e0b\u9762\u8fd9\u6837\u7684\u8bed\u53e5\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfunc = decorator(func)\r\n<\/pre>\n<p>\u5176\u5b9e\u5c31\u662f\u628a\u4e00\u4e2a\u51fd\u6570\u5f53\u53c2\u6570\u4f20\u5230\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u7136\u540e\u518d\u56de\u8c03\uff0c\u4f46\u662f\u503c\u5f97\u6ce8\u610f\u7684\u662f\u88c5\u9970\u5668\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u7ed9func<\/p>\n<p>\u88c5\u9970\u5668\u7684\u4e00\u5927\u7279\u6027\u662f\uff0c\u80fd\u628a\u88ab\u88c5\u9970\u7684\u51fd\u6570\u66ff\u6362\u6210\u5176\u4ed6\u51fd\u6570\u3002\u7b2c\u4e8c\u5927\u7279\u6027\u662f\uff0c\u88c5\u9970\u5668\u5728\u52a0\u8f7d\u6a21\u5757\u65f6\u7acb\u5373\u6267\u884c\u3002<\/p>\n<h2>\u88c5\u9970\u5668\u4f55\u65f6\u6267\u884c<\/h2>\n<p>\u88c5\u9970\u5668\u7684\u4e00\u4e2a\u5173\u952e\u7279\u6027\u662f\uff0c\u5b83\u4eec\u5728\u88ab\u88c5\u9970\u7684\u51fd\u6570\u5b9a\u4e49\u540e\u7acb\u5373\u8fd0\u884c\u3002\u8fd9\u901a\u5e38\u5728\u5bfc\u5165\u662f\uff08python \u52a0\u8f7d\u6a21\u5757\u65f6\uff09\u3002<\/p>\n<p>\u770b\u4e0b\u4e0b\u9762\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nregistry = &#x5B;]  # registry \u4fdd\u5b58\u88ab@register \u88c5\u9970\u7684\u51fd\u6570\u7684\u5f15\u7528\r\n\r\ndef register(func):  # register \u7684\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\r\n    print('running register(%s)' % func)  # \u6253\u5370\u88ab\u88c5\u9970\u7684\u51fd\u6570\r\n    registry.append(func)  # \u628a func \u5b58\u5165 `registery`\r\n    return func  # \u8fd4\u56de func\uff1a\u5fc5\u987b\u8fd4\u56de\u51fd\u6570\uff0c\u8fd9\u91cc\u8fd4\u56de\u7684\u51fd\u6570\u4e0e\u901a\u8fc7\u53c2\u6570\u4f20\u5165\u7684\u4e00\u6837\r\n\r\n@register  # `f1` \u548c `f2`\u88ab `@register` \u88c5\u9970\r\ndef f1():\r\n    print('running f1()')\r\n\r\n@register\r\ndef f2():\r\n    print('running f2()')\r\n\r\ndef f3():  # &lt;7&gt;\r\n    print('running f3()')\r\n\r\ndef main():  # main \u6253\u5370 `registry`\uff0c\u7136\u540e\u8c03\u7528 f1()\u3001f2()\u548c f3()\r\n    print('running main()')\r\n    print('registry -&gt;', registry)\r\n    f1()\r\n    f2()\r\n    f3()\r\n\r\nif __name__=='__main__':\r\n    main()  # &lt;9&gt;\r\n<\/pre>\n<p>\u8fd0\u884c\u4ee3\u7801\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nrunning register(&lt;function f1 at 0x1023fb378&gt;)\r\nrunning register(&lt;function f2 at 0x1023fb400&gt;)\r\nrunning main()\r\nregistry -&gt; &#x5B;&lt;function f1 at 0x1023fb378&gt;, &lt;function f2 at 0x1023fb400&gt;]\r\nrunning f1()\r\nrunning f2()\r\nrunning f3()\r\n<\/pre>\n<p>\u4ece\u7ed3\u679c\u53ef\u4ee5\u53d1\u73b0register \u5728\u6a21\u5757\u4e2d\u5176\u4ed6\u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u4e86\u4e24\u6b21\u3002\u8c03\u7528 register \u65f6\uff0c\u4f20\u7ed9\u5b83\u7684\u53c2\u6570\u662f\u88ab\u88c5\u9970\u7684\u51fd\u6570\uff08\u4f8b\u5982\uff09\u3002<\/p>\n<p>\u770b\u5b8c\u4e0a\u8fb9\u7684\u793a\u4f8b\u6211\u4eec\u77e5\u9053\uff0c\u51fd\u6570\u88ab\u88c5\u9970\u5668\u88c5\u9970\u540e\u4f1a\u53d8\u6210\u88c5\u9970\u5668\u51fd\u6570\u7684\u4e00\u4e2a\u53c2\u6570\uff0c\u90a3\u8fd9\u65f6\u5c31\u4e0d\u5f97\u4e0d\u8bf4\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e86\u3002<\/p>\n<h2>\u53d8\u91cf\u4f5c\u7528\u57df<\/h2>\n<p>\u5148\u770b\u4e0b\u4e0b\u8fb9\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef f1(a):\r\n    print(locals())\r\n    print(a)\r\n    print(b)\r\n    \r\n    \r\nf1(3)\r\n# output\r\n{'a': 3}\r\n3\r\nTraceback(most recent call last):\r\n    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\r\n    File &quot;&lt;stdin&gt;&quot;, line 3, in f1\r\nNameError: global name 'b' is not defined\r\n<\/pre>\n<p>\u8fd9\u91cc\u7684\u9519\u8bef\u662f\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf b \u6ca1\u6709\u5b9a\u4e49\uff0c\u5982\u679c\u6211\u4eec\u5148\u5728\u51fd\u6570\u5916\u90e8\u7ed9 b \u8d4b\u503c\uff0c\u518d\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4e0d\u4f1a\u62a5\u9519\u4e86\u3002<\/p>\n<p>\u51fd\u6570\u8fd0\u884c\u65f6\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\uff08\u547d\u540d\u7a7a\u95f4\uff09\u3002\u51fd\u6570\u7684\u547d\u540d\u7a7a\u95f4\u968f\u7740\u51fd\u6570\u8c03\u7528\u5f00\u59cb\u800c\u5f00\u59cb\uff0c\u7ed3\u675f\u800c\u9500\u6bc1\u3002<\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d f1 \u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u53ea\u6709 {&#8216;a&#8217;: 3}\uff0c\u6240\u4ee5 b \u4f1a\u88ab\u8ba4\u4e3a\u662f\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<p>\u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nb = 6\r\ndef f2(a):\r\n    print(a)\r\n    print(globals())\r\n    print(locals())\r\n    print(b)\r\n    b = 9\r\n    \r\nf2(3)\r\n# output\r\n3\r\n{\r\n    '__name__': '__main__',\r\n    '__doc__': None, \r\n    '__package__': None, \r\n    '__loader__': &lt;_frozen_importlib_external.SourceFileLoader object at 0x10c7f2dd8&gt;, \r\n    '__spec__': None, \r\n    '__annotations__': {}, \r\n    '__builtins__': &lt;module 'builtins' (built-in)&gt;, \r\n    '__file__': '~\/var_local.py', \r\n    '__cached__': None, \r\n    'b': 6, \r\n    'f2': &lt;function f2 at 0x10c7e7598&gt;\r\n}\r\n{'a': 3}\r\n3\r\nTraceback(most recent call last):\r\n    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\r\n    File &quot;&lt;stdin&gt;&quot;, line 3, in f1\r\nUnboundLocalError: local variable 'b' referenced before assignment\r\n<\/pre>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u548c\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e0d\u540c\u662f\uff0c\u6211\u73b0\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u4e86\u5168\u5c40\u53d8\u91cfb\uff0c\u4f46\u662f\u6267\u884cf2 \u8fd9\u4e2a\u65b9\u6cd5\u5e76\u6ca1\u6709\u6253\u53706\uff0c\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\u6267\u884c\u51fd\u6570\u65f6 Python \u4f1a\u5c1d\u8bd5\u4ece\u5c40\u90e8\u53d8\u91cf\u4e2d\u83b7\u53d6 b\uff0c\u51fd\u6570\u5bf9\u4e8e\u5df2\u7ecf\u5f15\u7528\u4f46\u672a\u8d4b\u503c\u7684\u53d8\u91cf\u5e76\u4e0d\u4f1a\u81ea\u52a8\u58f0\u660e\u4e3a\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5\u89e3\u91ca\u5668\u53d1\u73b0\u540e\u8fb9\u7684\u8d4b\u503c\u4e4b\u524d\u6709\u5f15\u7528\u5c31\u4f1a\u629b\u51faUnboundLocalError \u9519\u8bef\u3002<\/p>\n<p>Python \u4e0d\u8981\u6c42\u58f0\u660e\u53d8\u91cf\uff0c\u4f46\u662f\u5047\u5b9a\u5728\u51fd\u6570\u5b9a\u4e49\u4f53\u4e2d\u8d4b\u503c\u7684\u53d8\u91cf\u662f\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<p>\u5982\u679c\u8981\u8ba9\u89e3\u91ca\u5668\u628ab\u5f53\u505a\u5168\u5c40\u53d8\u91cf\uff0c\u8981\u4f7f\u7528global\u58f0\u660e\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nb = 6\r\ndef f3(a):\r\n    global b\r\n    print(a)\r\n    print(b)\r\n    b = 9\r\n    \r\nf2(3)\r\n# output\r\n3\r\n6\r\n<\/pre>\n<h1>\u95ed\u5305<\/h1>\n<p>\u95ed\u5305\u662f\u4e00\u79cd\u51fd\u6570\uff0c\u5b83\u4f1a\u4fdd\u7559\u5b9a\u4e49\u51fd\u6570\u65f6\u5b58\u5728\u7684\u81ea\u7531\u53d8\u91cf\u7684\u7ed1\u5b9a\uff0c\u8fd9\u6837\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u867d\u7136\u5b9a\u4e49\u4f5c\u7528\u57df\u4e0d\u53ef\u7528\uff0c\u4f46\u4ecd\u80fd\u4f7f\u7528\u90a3\u4e9b\u7ed1\u5b9a\u3002<\/p>\n<p>\u4ecb\u7ecd\u95ed\u5305\u524d\u5148\u8981\u8bf4\u660e\u4e00\u4e0b Python \u7684\u51fd\u6570\u53c2\u6570<\/p>\n<h2>\u51fd\u6570\u7684\u4e24\u79cd\u53c2\u6570<\/h2>\n<p>\u51fd\u6570\u6709\u4e24\u79cd\u53c2\u6570<\/p>\n<ol>\n<li>\u4f4d\u7f6e\u53c2\u6570<\/li>\n<li>\u547d\u540d\u53c2\u6570<\/li>\n<\/ol>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef foo(x, y=0):\r\n    return x - y\r\n<\/pre>\n<h2>python \u4e2d\u4e00\u5207\u90fd\u662f\u5bf9\u8c61<\/h2>\n<p>\u51fd\u6570\u548cpython\u4e2d\u5176\u4ed6\u4e00\u6837\u90fd\u662f\u5bf9\u8c61<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nIn&#x5B;7]: class A(object):\r\n   ...:     pass\r\n\r\n\r\nIn&#x5B;8]: A\r\nOut&#x5B;8]: __main__.A\r\nIn&#x5B;9]: type(A)\r\nOut&#x5B;9]: type\r\nIn&#x5B;10]: def foo():\r\n   ....:     pass\r\n\r\n\r\nIn&#x5B;11]: type(foo)\r\nOut&#x5B;11]: function\r\n\r\nIn&#x5B;12]: A.__class__\r\nOut&#x5B;12]: type\r\n\r\nIn&#x5B;13]: foo.__class__\r\nOut&#x5B;13]: function\r\n\r\nIn&#x5B;14]: a = 1\r\nIn&#x5B;15]: a.__class__\r\nOut&#x5B;15]: int\r\n\r\n# \u7c7b \u662f\u5bf9\u8c61\r\nIn&#x5B;16]: issubclass(A.__class__, object)\r\nOut&#x5B;16]: True\r\n\r\n# \u53d8\u91cf \u662f\u5bf9\u8c61\r\nIn&#x5B;17]: issubclass(a.__class__, object)\r\nOut&#x5B;17]: True\r\n\r\n# \u51fd\u6570 \u662f\u5bf9\u8c61\r\nIn&#x5B;18]: issubclass(foo.__class__, object)\r\nOut&#x5B;18]: True\r\n<\/pre>\n<p>\u6240\u4ee5\u51fd\u6570\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u5b83\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u88ab\u5f53\u505a\u8fd4\u56de\u503c\u8fd4\u56de<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef add(x, y):\r\n    return x + y\r\n\r\ndef apply(func):\r\n    return func\r\n\r\n&gt;&gt; a = apply(add)\r\n&gt;&gt; type(a)\r\n&lt;type 'function'&gt;\r\n\r\n&gt;&gt; a(1, 2)\r\n&gt;&gt; 3\r\n<\/pre>\n<h2>\u95ed\u5305\u7684\u4f7f\u7528<\/h2>\n<p>\u5148\u6765\u770b\u4e00\u4e2a\u793a\u4f8b\uff1a\u5047\u8bbe\u6709\u4e2a\u540d\u4e3a avg \u7684\u51fd\u6570\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u8ba1\u7b97\u4e0d\u65ad\u589e\u52a0\u7684\u7cfb\u5217\u503c\u7684\u5747\u503c\uff1b<\/p>\n<p>\u5b83\u662f\u8fd9\u4e48\u4f7f\u7528\u7684\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; avg(10)\r\n10\r\n&gt;&gt;&gt; avg(11)\r\n10.5\r\n&gt;&gt;&gt; avg(12)\r\n11\r\n<\/pre>\n<p>\u90a3\u4e48\u6211\u4eec\u8003\u8651\u4e0b\uff0cavg \u4ece\u4f55\u800c\u6765\uff0c\u5b83\u53c8\u5728\u54ea\u91cc\u4fdd\u5b58\u5386\u53f2\u503c\u5462\uff0c\u8fd9\u4e2a\u7528\u95ed\u5305\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<br \/>\n\u4e0b\u8fb9\u7684\u4ee3\u7801\u662f\u95ed\u5305\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef make_averager():\r\n    series = &#x5B;]\r\n\r\n    def averager(new_value):\r\n        series.append(new_value)\r\n        total = sum(series)\r\n        return total\/len(series)\r\n\r\n    return averager\r\n<\/pre>\n<p>\u8c03\u7528 make_averager \u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a averager \u51fd\u6570\u5bf9\u8c61\u3002\u6bcf\u6b21\u8c03\u7528 averager \u65f6\uff0c\u5b83\u90fd\u4f1a\u628a\u53c2\u6570\u6dfb\u52a0\u5230\u7cfb\u5217\u503c\u4e2d\uff0c\u7136\u540e\u8ba1\u7b97\u5f53\u524d\u5e73\u5747\u503c\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\navg = make_averager()\r\n\r\n&gt;&gt;&gt; avg(10)\r\n10\r\n&gt;&gt;&gt; avg(11)\r\n10.5\r\n&gt;&gt;&gt; avg(12)\r\n11\r\n<\/pre>\n<p>series \u662fmake_averager \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u56e0\u4e3a\u90a3\u4e2a\u51fd\u6570\u7684\u5b9a\u4e49\u4f53\u4e2d\u521d\u59cb\u5316\u4e86series: series=[]\u3002\u4f46\u5728averager \u51fd\u6570\u4e2d\uff0cseries \u662f\u81ea\u7531\u53d8\u91cf\uff08\u6307\u672a\u5728\u672c\u5730\u4f5c\u7528\u57df\u4e2d\u7ed1\u5b9a\u7684\u53d8\u91cf\uff09\u3002<\/p>\n<p>averager \u7684\u95ed\u5305\u5ef6\u4f38\u5230\u90a3\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e4b\u5916\uff0c\u5305\u542b\u81ea\u7531\u53d8\u91cfseries\u7684\u7ed1\u5b9a\u3002<\/p>\n<p>avg \u5c31\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u4e5f\u53ef\u4ee5\u8bf4 make_averager \u6307\u5411\u4e00\u4e2a\u95ed\u5305\uff0c\u6216\u8005\u8bf4 make_averager \u662f\u95ed\u5305\u7684\u5de5\u5382\u51fd\u6570<\/p>\n<p>\u95ed\u5305\u53ef\u4ee5\u8ba4\u4e3a\u662f\u4e00\u4e2a\u5185\u5c42\u51fd\u6570(averager)\uff0c\u7531\u4e00\u4e2a\u53d8\u91cf\u6307\u4ee3\uff0c\u800c\u8fd9\u4e2a\u53d8\u91cf\u76f8\u5bf9\u4e8e\u5916\u5c42\u5305\u542b\u5b83\u7684\u51fd\u6570\u800c\u8a00\uff0c\u662f\u672c\u5730\u53d8\u91cf\u5d4c\u5957\u5b9a\u4e49\u5728\u975e\u5168\u5c40\u4f5c\u7528\u57df\u91cc\u9762\u7684\u51fd\u6570\u80fd\u591f\u8bb0\u4f4f\u5b83\u5728\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\u5b83\u6240\u5904\u7684\u5c01\u95ed\u547d\u540d\u7a7a\u95f4<\/p>\n<p>\u95ed\u5305 \u53ea\u662f\u5728\u5f62\u5f0f\u548c\u8868\u73b0\u4e0a\u50cf\u51fd\u6570\uff0c\u4f46\u5b9e\u9645\u4e0a\u4e0d\u662f\u51fd\u6570\u3002\u51fd\u6570\u662f\u4e00\u4e9b\u53ef\u6267\u884c\u7684\u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u5728\u51fd\u6570\u88ab\u5b9a\u4e49\u540e\u5c31\u786e\u5b9a\u4e86\uff0c\u4e0d\u4f1a\u5728\u6267\u884c\u65f6\u53d1\u751f\u53d8\u5316\uff0c\u6240\u4ee5\u4e00\u4e2a\u51fd\u6570\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\u95ed\u5305\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u6709\u591a\u4e2a\u5b9e\u4f8b\uff0c\u4e0d\u540c\u7684\u5f15\u7528\u73af\u5883\u548c\u76f8\u540c\u7684\u51fd\u6570\u7ec4\u5408\u53ef\u4ee5\u4ea7\u751f\u4e0d\u540c\u7684\u5b9e\u4f8b\u3002<\/p>\n<h1>\u88c5\u9970\u5668<\/h1>\n<h2>\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u88c5\u9970\u5668<\/h2>\n<p>\u5bf9\u4e00\u4e2a\u5df2\u6709\u7684\u6a21\u5757\u505a\u4e00\u4e9b\u201c\u4fee\u9970\u5de5\u4f5c\u201d\uff0c\u6240\u8c13\u4fee\u9970\u5de5\u4f5c\u5c31\u662f\u60f3\u7ed9\u73b0\u6709\u7684\u6a21\u5757\u52a0\u4e0a\u4e00\u4e9b\u5c0f\u88c5\u9970\uff08\u4e00\u4e9b\u5c0f\u529f\u80fd\uff0c\u8fd9\u4e9b\u5c0f\u529f\u80fd\u53ef\u80fd\u597d\u591a\u6a21\u5757\u90fd\u4f1a\u7528\u5230\uff09\uff0c\u4f46\u53c8\u4e0d\u8ba9\u8fd9\u4e2a\u5c0f\u88c5\u9970\uff08\u5c0f\u529f\u80fd\uff09\u4fb5\u5165\u5230\u539f\u6709\u7684\u6a21\u5757\u4e2d\u7684\u4ee3\u7801\u91cc\u53bb<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef my_decorator(func):\r\n    def wrapper():\r\n        print &quot;Before the function runs&quot;\r\n        func() # \u8fd9\u884c\u4ee3\u7801\u53ef\u7528\uff0c\u662f\u56e0\u4e3a wrapper \u7684\u95ed\u5305\u4e2d\u5305\u542b\u81ea\u7531\u53d8\u91cf func\r\n        print &quot;After the function runs&quot;\r\n    return wrapper\r\n\r\ndef my_func():\r\n    print &quot;I am a stand alone function&quot;\r\n\r\n&gt;&gt; my_func()\r\n\r\n# output\r\nI am a stand alone function\r\n\r\n# \u7136\u540e\uff0c\u6211\u4eec\u5728\u8fd9\u91cc\u88c5\u9970\u8fd9\u4e2a\u51fd\u6570\r\n# \u5c06\u51fd\u6570\u4f20\u9012\u7ed9\u88c5\u9970\u5668\uff0c\u88c5\u9970\u5668\u5c06\u52a8\u6001\u5730\u5c06\u5176\u5305\u88c5\u5728\u4efb\u4f55\u60f3\u6267\u884c\u7684\u4ee3\u7801\u4e2d\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\r\n\r\n&gt;&gt; my_func = my_decorator(my_func)\r\n&gt;&gt; my_func()\r\n#output\r\nBefore the function runs\r\nI am a stand alone function\r\nAfter the function runs\r\n\r\n# \u4e5f\u53ef\u4ee5\u8fd9\u4e48\u5199\r\n\r\n@ my_decorator\r\ndef my_func():\r\n    print &quot;I am a stand alone function&quot;\r\n\r\n&gt;&gt; my_func()\r\n#output\r\nBefore the function runs\r\nI am a stand alone function\r\nAfter the function runs\r\n<\/pre>\n<p>\u88c5\u9970\u5668\u662f\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u88c5\u9970\u5668\u6a21\u5f0f\uff08\u82f1\u6587\u7248\uff09\u7684python\u5b9e\u73b0\u3002<\/p>\n<h2>\u591a\u4e2a\u88c5\u9970\u5668<\/h2>\n<p>\u88c5\u9970\u5668\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef bread(func):\r\n    def wrapper():\r\n        print &quot;&lt;\/''''''\\&gt;&quot;\r\n        func()\r\n        print &quot;&lt;\\______\/&gt;&quot;\r\n    return wrapper\r\n\r\ndef ingredients(func):\r\n    def wrapper():\r\n        print &quot;#tomatoes#&quot;\r\n        func()\r\n        print &quot;~salad~&quot;\r\n    return wrapper\r\n\r\ndef sandwich(food=&quot;--ham--&quot;):\r\n    print food\r\n\r\n#### outputs:\r\n<\/pre>\n<h2>\u5d4c\u5957\u4e24\u4e2a\u88c5\u9970\u5668<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt; sandwich = bread(ingredients(sandwich))\r\n&gt;&gt; sandwich()\r\n\r\n#### outputs\r\n&lt;\/''''''\\&gt;\r\n #tomatoes#\r\n --ham--\r\n ~salad~\r\n&lt;\\______\/&gt;\r\n<\/pre>\n<p>\u66f4\u7b80\u5355\u7684\u5199\u6cd5<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@bread\r\n@ingredients\r\ndef sandwich(food=&quot;--ham--&quot;):\r\n    print food\r\n<\/pre>\n<p>\u88c5\u9970\u5668\u7684\u987a\u5e8f\u662f\u5f88\u91cd\u8981\u7684<\/p>\n<p>\u5982\u679c\u6211\u4eec\u6362\u4e0b\u987a\u5e8f\u5c31\u4f1a\u53d1\u73b0\uff0c\u4e09\u660e\u6cbb\u53d8\u6210\u4e86\u62ab\u8428\u3002\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@ingredients\r\n@bread\r\ndef sandwich(food=&quot;--ham--&quot;):\r\n    print food\r\n\r\n# outputs:\r\n    \r\n #tomatoes#\r\n &lt;\/' ' ' ' ' '\\&gt;\r\n   --ham--\r\n &lt;\\______\/&gt;\r\n ~salad~\r\n<\/pre>\n<h2>Decorator \u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u9996\u5148\u770b\u4e00\u4e0b\u8fd9\u6bb5\u4ee3\u7801<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef deco(fn):\r\n    print &quot;I am %s!&quot; % fn.__name__\r\n\r\n@deco\r\ndef func():\r\n    pass\r\n\r\n# output\r\nI am func!\r\n\r\n# \u6ca1\u6709\u6267\u884cfunc \u51fd\u6570 \u4f46\u662f deco \u88ab\u6267\u884c\u4e86\r\n<\/pre>\n<p>\u5728\u7528\u67d0\u4e2a@decorator\u6765\u4fee\u9970\u67d0\u4e2a\u51fd\u6570func\u65f6<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@decorator\r\ndef func():\r\n    pass\r\n<\/pre>\n<p>\u5176\u89e3\u91ca\u5668\u4f1a\u89e3\u91ca\u6210\u4e0b\u9762\u8fd9\u6837\u7684\u8bed\u53e5\uff1a<\/p>\n<p>func = decorator(func)<\/p>\n<p>\u5176\u5b9e\u5c31\u662f\u628a\u4e00\u4e2a\u51fd\u6570\u5f53\u53c2\u6570\u4f20\u5230\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u7136\u540e\u518d\u56de\u8c03\u3002\u4f46\u662f\u503c\u5f97\u6ce8\u610f\u7684\u662f\u88c5\u9970\u5668\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u7ed9func<\/p>\n<p>\u56de\u5230\u521a\u624d\u7684\u4f8b\u5b50<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef my_decorator(func):\r\n    def wrapper():\r\n        print &quot;Before the function runs&quot;\r\n        func()\r\n        print &quot;After the function runs&quot;\r\n    return wrapper\r\n\r\ndef my_func():\r\n    print &quot;I am a stand alone function&quot;\r\n\r\n&gt;&gt; my_func = my_decorator(my_func)\r\n&gt;&gt; my_func()\r\n#output\r\nBefore the function runs\r\nI am a stand alone function\r\nAfter the function runs\r\n<\/pre>\n<p>my_decorator(my_func)\u8fd4\u56de\u4e86wrapper()\u51fd\u6570\uff0c\u6240\u4ee5\uff0cmy_func\u5176\u5b9e\u53d8\u6210\u4e86wrapper\u7684\u4e00\u4e2a\u53d8\u91cf\uff0c\u800c\u540e\u9762\u7684my_func()\u6267\u884c\u5176\u5b9e\u53d8\u6210\u4e86wrapper()<\/p>\n<p>\u6bd4\u5982\uff1a\u591a\u4e2adecorator<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@decorator_one\r\n@decorator_two\r\ndef func():\r\n    pass\r\n<\/pre>\n<p>\u76f8\u5f53\u4e8e\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfunc = decorator_one(decorator_two(func))\r\n<\/pre>\n<p>\u6bd4\u5982\uff1a\u5e26\u53c2\u6570\u7684decorator\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@decorator(arg1, arg2)\r\ndef func():\r\n    pass\r\n# \u76f8\u5f53\u4e8e\uff1a\r\n\r\nfunc = decorator(arg1,arg2)(func)\r\n<\/pre>\n<h2>\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668<\/h2>\n<p>\u9996\u5148\u770b\u4e00\u4e0b\uff0c \u5982\u679c\u88ab\u88c5\u9970\u7684\u65b9\u6cd5\u6709\u53c2\u6570<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef a_decorator(method_to_decorate):\r\n    def wrapper(self, x):\r\n        x -= 3\r\n        print 'x is %s' % x\r\n        method_to_decorate(self, x)\r\n    return wrapper\r\n\r\n\r\nclass A(object):\r\n\r\n    def __init__(self):\r\n        self.b = 42\r\n\r\n    @a_decorator\r\n    def number(self, x):\r\n        print &quot;b is %s&quot; % (self.b + x)\r\n\r\n\r\na = A()\r\na.number(-3)\r\n\r\n# output\r\nx is -6\r\nb is 36\r\n<\/pre>\n<p>\u901a\u5e38\u6211\u4eec\u90fd\u4f7f\u7528\u66f4\u52a0\u901a\u7528\u7684\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u4f5c\u7528\u5728\u4efb\u4f55\u51fd\u6570\u6216\u5bf9\u8c61\u65b9\u6cd5\u4e0a\uff0c\u800c\u4e0d\u5fc5\u5173\u5fc3\u5176\u53c2\u6570\u4f7f\u7528<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef a_decorator(method_to_decorate):\r\n    def wrapper(*args, **kwargs):\r\n        print '****** args ******'\r\n        print args\r\n        print kwargs\r\n        method_to_decorate(*args, **kwargs)\r\n    return wrapper\r\n\r\n@a_decorator\r\ndef func():\r\n    pass\r\n\r\nfunc()\r\n#output\r\n****** args ******\r\n()\r\n{}\r\n\r\n@a_decorator\r\ndef func_with_args(a, b=0):\r\n    pass\r\n    return a + b\r\n\r\nfunc_with_args(1, b=2)\r\n\r\n#output\r\n****** args ******\r\n(1,)\r\n{'b': 2}\r\n<\/pre>\n<p>\u4e0a\u8fb9\u7684\u793a\u4f8b\u662f\u5e26\u53c2\u6570\u7684\u88ab\u88c5\u9970\u51fd\u6570<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u770b\u4e00\u4e0b\u5411\u88c5\u9970\u5668\u672c\u8eab\u4f20\u9012\u53c2\u6570<\/p>\n<h2>\u5411\u88c5\u9970\u5668\u672c\u8eab\u4f20\u9012\u53c2\u6570<\/h2>\n<p>\u88c5\u9970\u5668\u5fc5\u987b\u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u4f60\u4e0d\u80fd\u76f4\u63a5\u4f20\u9012\u53c2\u6570\u7ed9\u88c5\u9970\u5668\u672c\u8eab<\/p>\n<p>\u5982\u679c\u60f3\u4f20\u9012\u53c2\u6570\u7ed9\u88c5\u9970\u5668\uff0c\u53ef\u4ee5 \u58f0\u660e\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u88c5\u9970\u5668\u7684\u51fd\u6570<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# \u6211\u662f\u4e00\u4e2a\u521b\u5efa\u88c5\u9970\u5668\u7684\u51fd\u6570\r\ndef decorator_maker():\r\n    print &quot;I make decorators!&quot;\r\n\r\n    def my_decorator(func):\r\n        print &quot;I am a decorator!&quot;\r\n\r\n        def wrapped():\r\n            print &quot;I am the wrapper around the decorated function. &quot;\r\n            return func()\r\n\r\n        print &quot;As the decorator, I return the wrapped function.&quot;\r\n        return wrapped\r\n\r\n    print &quot;As a decorator maker, I return a decorator&quot;\r\n    return my_decorator\r\n\r\n# decorator_maker()\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u88c5\u9970\u5668\r\nnew_deco = decorator_maker()\r\n\r\n#outputs\r\nI make decorators!\r\nAs a decorator maker, I return a decorator\r\n\r\n# \u4f7f\u7528\u88c5\u9970\u5668\r\ndef decorated_function():\r\n    print &quot;I am the decorated function&quot;\r\n\r\ndecorated_function = new_deco(decorated_function)   \r\ndecorated_function()\r\n\r\n# outputs\r\nI make decorators!\r\nAs a decorator maker, I return a decorator\r\nI am a decorator!\r\nAs the decorator, I return the wrapped function.\r\nI am the wrapper around the decorated function.\r\nI am the decorated  function\r\n<\/pre>\n<p>\u4f7f\u7528@\u4fee\u9970<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndecorated_function = new_deco(decorated_function)\r\n\r\n# \u7b49\u4ef7\u4e8e\u4e0b\u9762\u7684\u65b9\u6cd5\r\n\r\n@new_deco\r\ndef func():\r\n    print &quot;I am the decorated function&quot;\r\n\r\n\r\n@decorator_maker()\r\ndef func():\r\n    print &quot;I am the decorated function&quot;\r\n<\/pre>\n<p>my_decorator\uff08\u88c5\u9970\u5668\u51fd\u6570\uff09\u662fdecorator_maker\uff08\u88c5\u9970\u5668\u751f\u6210\u51fd\u6570\uff09\u7684\u5185\u90e8\u51fd\u6570<\/p>\n<p>\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528\u628a\u53c2\u6570\u52a0\u5728decorator_maker\uff08\u88c5\u9970\u5668\u751f\u6210\u51fd\u6570\uff09\u7684\u65b9\u6cd5\u50cf\u88c5\u9970\u5668\u4f20\u9012\u53c2\u6570<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# \u6211\u662f\u4e00\u4e2a\u521b\u5efa\u5e26\u53c2\u6570\u88c5\u9970\u5668\u7684\u51fd\u6570\r\ndef decorator_maker_with_arguments(darg1, darg2):\r\n    print &quot;I make decorators! And I accept arguments:&quot;, darg1, darg2\r\n\r\n    def my_decorator(func):\r\n        print &quot;I am a decorator! Somehow you passed me arguments:&quot;, darg1, darg2\r\n\r\n        def wrapped(farg1, farg2):\r\n            print &quot;I am the wrapper around the decorated function.&quot;\r\n            print &quot;I can access all the variables&quot;, darg1, darg2, farg1, farg2\r\n            return func(farg1, farg2)\r\n\r\n        print &quot;As the decorator, I return the wrapped function.&quot;\r\n        return wrapped\r\n\r\n    print &quot;As a decorator maker, I return a decorator&quot;\r\n    return my_decorator\r\n\r\n@decorator_maker_with_arguments(&quot;deco_arg1&quot;, &quot;deco_arg2&quot;)\r\ndef decorated_function_with_arguments(function_arg1, function_arg2):\r\n    print (&quot;I am the decorated function and only knows about my arguments: {0}&quot;\r\n           &quot; {1}&quot;.format(function_arg1, function_arg2))\r\n\r\n\r\ndecorated_function_with_arguments('farg1', 'farg2')\r\n\r\n# outputs\r\n\r\nI make decorators And I accept arguments: deco_arg1 deco_arg2\r\nAs a decorator maker, I return a decorator\r\nI am a decorator Somehow you passed me arguments: deco_arg1 deco_arg2\r\nAs the decorator, I return the wrapped function.\r\nI am the wrapper around the decorated function.\r\nI can access all the variables deco_arg1 deco_arg2 farg1 farg2\r\nI am the decorated function and only knows about my arguments: farg1 farg2   \r\n<\/pre>\n<p>\u8fd9\u91cc\u88c5\u9970\u5668\u751f\u6210\u51fd\u6570\u5185\u90e8\u4f20\u9012\u53c2\u6570\u662f\u95ed\u5305\u7684\u7279\u6027<\/p>\n<h2>\u4f7f\u7528\u88c5\u9970\u5668\u9700\u8981\u6ce8\u610f<\/h2>\n<ul>\n<li>\u88c5\u9970\u5668\u662fPython2.4\u7684\u65b0\u7279\u6027<\/li>\n<li>\u88c5\u9970\u5668\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u6027\u80fd<\/li>\n<li>\u88c5\u9970\u5668\u4ec5\u5728Python\u4ee3\u7801\u5bfc\u5165\u65f6\u88ab\u8c03\u7528\u4e00\u6b21,\u4e4b\u540e\u4f60\u4e0d\u80fd\u52a8\u6001\u5730\u6539\u53d8\u53c2\u6570.\u5f53\u4f60\u4f7f\u7528&#8221;import x&#8221;,\u51fd\u6570\u5df2\u7ecf\u88ab\u88c5\u9970<\/li>\n<\/ul>\n<p>\u4f7f\u7528 functools.wraps<\/p>\n<p>\u6700\u540ePython2.5\u89e3\u51b3\u4e86\u6700\u540e\u4e00\u4e2a\u95ee\u9898\uff0c\u5b83\u63d0\u4f9bfunctools\u6a21\u5757\uff0c\u5305\u542bfunctools.wraps\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5c06\u88ab\u88c5\u9970\u51fd\u6570\u7684\u540d\u79f0\u3001\u6a21\u5757\u3001\u6587\u6863\u5b57\u7b26\u4e32\u62f7\u8d1d\u7ed9\u5c01\u88c5\u51fd\u6570<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef foo():\r\n    print &quot;foo&quot;\r\n\r\nprint foo.__name__\r\n#outputs: foo\r\n\r\n# \u4f46\u5f53\u4f60\u4f7f\u7528\u88c5\u9970\u5668\r\ndef bar(func):\r\n    def wrapper():\r\n        print &quot;bar&quot;\r\n        return func()\r\n    return wrapper\r\n\r\n@bar\r\ndef foo():\r\n    print &quot;foo&quot;\r\n\r\nprint foo.__name__\r\n#outputs: wrapper\r\n<\/pre>\n<p>&#8220;functools&#8221; \u53ef\u4ee5\u4fee\u6b63\u8fd9\u4e2a\u9519\u8bef<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport functools\r\n\r\ndef bar(func):\r\n    # \u6211\u4eec\u6240\u8bf4\u7684 &quot;wrapper&quot;, \u5c01\u88c5 &quot;func&quot;\r\n    @functools.wraps(func)\r\n    def wrapper():\r\n        print &quot;bar&quot;\r\n        return func()\r\n    return wrapper\r\n\r\n@bar\r\ndef foo():\r\n    print &quot;foo&quot;\r\n\r\n# \u5f97\u5230\u7684\u662f\u539f\u59cb\u7684\u540d\u79f0, \u800c\u4e0d\u662f\u5c01\u88c5\u5668\u7684\u540d\u79f0\r\nprint foo.__name__\r\n#outputs: foo\r\n<\/pre>\n<h2>\u7c7b\u88c5\u9970\u5668<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass myDecorator(object):\r\n\r\n    def __init__(self, func):\r\n        print &quot;inside myDecorator.__init__()&quot;\r\n        self.func = func\r\n\r\n    def __call__(self):\r\n        self.func()\r\n        print &quot;inside myDecorator.__call__()&quot;\r\n\r\n@myDecorator\r\ndef aFunction():\r\n    print &quot;inside aFunction()&quot;\r\n\r\nprint &quot;Finished decorating aFunction()&quot;\r\n\r\naFunction()\r\n\r\n# output\uff1a\r\n# inside myDecorator.__init__()\r\n# Finished decorating aFunction()\r\n# inside aFunction()\r\n# inside myDecorator.__call__()\r\n<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u7c7b\u4e2d\u6709\u4e24\u4e2a\u6210\u5458\uff1a<\/p>\n<p>\u4e00\u4e2a\u662f__init__()\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5728\u6211\u4eec\u7ed9\u67d0\u4e2a\u51fd\u6570decorator\u65f6\u88ab\u8c03\u7528\uff0c\u6240\u4ee5\uff0c\u9700\u8981\u6709\u4e00\u4e2afunc\u7684\u53c2\u6570\uff0c\u4e5f\u5c31\u662f\u88abdecorator\u7684\u51fd\u6570\u3002<br \/>\n\u4e00\u4e2a\u662f__call__()\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5728\u6211\u4eec\u8c03\u7528\u88abdecorator\u51fd\u6570\u65f6\u88ab\u8c03\u7528\u7684<\/p>\n<p>\u5982\u679cdecorator\u6709\u53c2\u6570\u7684\u8bdd\uff0c__init__() \u5c31\u4e0d\u80fd\u4f20\u5165func\u4e86\uff0c\u800cfn\u662f\u5728__call__\u7684\u65f6\u5019\u4f20\u5165<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass myDecorator(object):\r\n\r\n    def __init__(self, arg1, arg2):\r\n        self.arg1 = arg2\r\n\r\n    def __call__(self, func):\r\n        def wrapped(*args, **kwargs):\r\n            return self.func(*args, **kwargs)\r\n        return wrapped\r\n<\/pre>\n<h1>\u88c5\u9970\u5668\u793a\u4f8b<\/h1>\n<p>Python \u5185\u7f6e\u4e86\u4e09\u4e2a\u7528\u4e8e\u88c5\u9970\u65b9\u6cd5\u7684\u51fd\u6570\uff1aproperty\u3001classmethod\u548c staticmethod\u3002<br \/>\n\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u88c5\u9970\u5668\u662f functools.wraps\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u534f\u52a9\u6784\u5efa\u884c\u4e3a\u826f\u597d\u7684\u88c5\u9970\u5668\u3002<\/p>\n<p>functools.lru_cache<\/p>\n<p>functools.lru_cache \u5b9e\u73b0\u4e86\u5185\u5b58\u7f13\u5b58\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u628a\u8017\u65f6\u957f\u7684\u51fd\u6570\u7ed3\u679c\u4fdd\u5b58\u8d77\u6765\uff0c\u907f\u514d\u4f20\u5165\u76f8\u540c\u53c2\u6570\u65f6\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<p>\u6211\u4eec\u81ea\u5df1\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom functools import wraps\r\ndef memo(fn):\r\n    cache = {}\r\n    miss = object()\r\n\r\n    @wraps(fn)\r\n    def wrapper(*args):\r\n        result = cache.get(args, miss)\r\n        if result is miss:\r\n            result = fn(*args)\r\n            print &quot;{0} has been used: {1}x&quot;.format(fn.__name__, wrapper.count)\r\n            cache&#x5B;args] = result\r\n        return result\r\n\r\n    return wrapper\r\n\r\n@memo\r\ndef fib(n):\r\n    if n &lt; 2:\r\n        return n\r\n    return fib(n - 1) + fib(n - 2)\r\n<\/pre>\n<p>\u7edf\u8ba1\u51fd\u6570\u6267\u884c\u6b21\u6570\u7684\u88c5\u9970\u5668<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef counter(func):\r\n    &quot;&quot;&quot;\r\n    \u8bb0\u5f55\u5e76\u6253\u5370\u4e00\u4e2a\u51fd\u6570\u7684\u6267\u884c\u6b21\u6570\r\n    &quot;&quot;&quot;\r\n    def wrapper(*args, **kwargs):\r\n        wrapper.count = wrapper.count + 1\r\n        res = func(*args, **kwargs)\r\n        print &quot;{0} has been used: {1}x&quot;.format(func.__name__, wrapper.count)\r\n        return res\r\n    wrapper.count = 0\r\n    return wrapper\r\n<\/pre>\n<p>\u88c5\u9970\u5668\u505a\u7f13\u5b58<\/p>\n<p>\u5e26\u6709\u8fc7\u671f\u65f6\u95f4\u7684\u5185\u5b58\u7f13\u5b58<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef cache_for(duration):\r\n    def deco(func):\r\n        @wraps(func)\r\n        def fn(*args, **kwargs):\r\n            key = pickle.dumps((args, kwargs))\r\n            value, expire = func.func_dict.get(key, (None, None))\r\n            now = int(time.time())\r\n            if value is not None and expire &amp;gt; now:\r\n                return value\r\n            value = func(*args, **kwargs)\r\n            func.func_dict&#x5B;key] = (value, int(time.time()) + duration)\r\n            return value\r\n        return fn\r\n    return deco\r\n<\/pre>\n<p>\u7edf\u8ba1\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef timeit(fn):\r\n\r\n    @wraps(fn)\r\n    def real_fn(*args, **kwargs):\r\n        if config.common&#x5B;'ENVIRON'] == 'PRODUCTION':\r\n            return fn(*args, **kwargs)\r\n\r\n        _start = time.time()\r\n        #app.logger.debug('Start timeit for %s' % fn.__name__)\r\n        result = fn(*args, **kwargs)\r\n        _end = time.time()\r\n        _last = _end - _start\r\n        app.logger.debug('End timeit for %s in %s seconds.' %\r\n                         (fn.__name__, _last))\r\n        return result\r\n\r\n    return real_fn\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u88c5\u9970\u5668\u57fa\u7840\u77e5\u8bc6 \u9996\u5148\u770b\u4e00\u4e0b\u8fd9\u6bb5\u4ee3\u7801 def deco(fn): print &quot;I am %s!&#038;qu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6294","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6294"}],"version-history":[{"count":4,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6294\/revisions"}],"predecessor-version":[{"id":6337,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6294\/revisions\/6337"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}