{"id":102,"date":"2026-08-06T00:29:59","date_gmt":"2026-08-05T16:29:59","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=102"},"modified":"2026-08-06T00:44:46","modified_gmt":"2026-08-05T16:44:46","slug":"day-18-%e8%bf%ad%e4%bb%a3%e5%99%a8%e3%80%81%e7%94%9f%e6%88%90%e5%99%a8%e4%b8%8e%e8%a3%85%e9%a5%b0%e5%99%a8%ef%bc%9a%e6%83%b0%e6%80%a7%e6%b1%82%e5%80%bc%e4%b8%8e-%e8%af%ad%e6%b3%95%e7%b3%96","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-18-%e8%bf%ad%e4%bb%a3%e5%99%a8%e3%80%81%e7%94%9f%e6%88%90%e5%99%a8%e4%b8%8e%e8%a3%85%e9%a5%b0%e5%99%a8%ef%bc%9a%e6%83%b0%e6%80%a7%e6%b1%82%e5%80%bc%e4%b8%8e-%e8%af%ad%e6%b3%95%e7%b3%96\/","title":{"rendered":"| Day 18 | \u8fed\u4ee3\u5668\u3001\u751f\u6210\u5668\u4e0e\u88c5\u9970\u5668\uff1a\u60f0\u6027\u6c42\u503c\u4e0e @ \u8bed\u6cd5\u7cd6 |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u8fed\u4ee3\u5668\u534f\u8bae \u2014\u2014 for \u5faa\u73af\u7684\u5e55\u540e\u771f\u76f8<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8fed\u4ee3\u5668<\/strong>\uff08iterator\uff09\u662f\u300c\u4e00\u6b21\u53d6\u4e00\u4e2a\u300d\u7684\u53d6\u503c\u5de5\u5177\u3002\u4f60\u5929\u5929\u7528\u7684 <code>for x in \u5217\u8868<\/code>\uff0c\u5e95\u5c42\u5c31\u662f\u5728\u548c\u8fed\u4ee3\u5668\u6253\u4ea4\u9053\uff1a<code>iter()<\/code> \u62ff\u8fed\u4ee3\u5668\uff0c<code>next()<\/code> \u9010\u4e2a\u53d6\u503c\uff0c\u53d6\u5b8c\u629b <code>StopIteration<\/code> \u7ed3\u675f\u3002\u7406\u89e3\u8fd9\u5957\u534f\u8bae\uff0c\u4f60\u5c31\u80fd\u8ba9<strong>\u4efb\u4f55\u81ea\u5b9a\u4e49\u7c7b<\/strong>\u88ab <code>for<\/code> \u904d\u5386\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u300c\u53ef\u8fed\u4ee3\u300d\uff08iterable\uff09\u4e0e\u300c\u8fed\u4ee3\u5668\u300d\uff08iterator\uff09\u662f\u4e24\u4e2a\u6982\u5ff5\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\uff09\u5b9e\u73b0\u4e86 <code>__iter__<\/code>\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff1b\u8fed\u4ee3\u5668\u5b9e\u73b0\u4e86 <code>__next__<\/code>\uff0c\u6bcf\u6b21\u8fd4\u56de\u4e0b\u4e00\u4e2a\u503c\uff0c\u53d6\u5b8c\u629b <code>StopIteration<\/code>\u3002<code>for<\/code> \u5faa\u73af\u7684\u5b8c\u6574\u5c55\u5f00\u662f\uff1a<code>it = iter(\u5bf9\u8c61)<\/code> \u2192 \u5faa\u73af\u91cc <code>x = next(it)<\/code> \u2192 \u6355\u83b7 <code>StopIteration<\/code> \u5c31 break\u3002\u8fed\u4ee3\u5668\u50cf\u300c\u4e00\u6b21\u6027\u5438\u7ba1\u300d\u2014\u2014\u53d6\u8fc7\u7684\u503c\u4e0d\u4fdd\u7559\uff0c\u6240\u4ee5<strong>\u8fed\u4ee3\u5668\u53ea\u80fd\u7528\u4e00\u904d<\/strong>\uff08<code>list(it)<\/code> \u4e4b\u540e\u518d <code>next(it)<\/code> \u5c31\u62a5\u9519\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fed\u4ee3\u5668\u50cf<strong>\u6392\u961f\u53d6\u9910\u7684\u7a97\u53e3<\/strong>\uff1a\u4f60\u6bcf\u6b21\u53bb\u95ee\u300c\u4e0b\u4e00\u4e2a\u597d\u4e86\u5417\u300d\uff08<code>next<\/code>\uff09\uff0c\u7a97\u53e3\u7ed9\u4f60\u4e00\u4efd\u9910\uff0c\u53d1\u5b8c\u4e86\u5c31\u544a\u8bc9\u4f60\u300c\u6ca1\u4e86\u300d\uff08<code>StopIteration<\/code>\uff09\u2014\u2014\u961f\u4f0d\u8fd8\u5728\u539f\u5730\uff0c\u4f46\u4f60\u7684\u53d6\u9910\u8fc7\u7a0b\u8d70\u5b8c\u5c31\u7ed3\u675f\u4e86\uff0c\u60f3\u518d\u53d6\u5f97\u91cd\u65b0\u6392\u961f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>iter \u62ff\u8fed\u4ee3\u5668\uff0cnext \u9010\u4e2a\u53d6\uff1b\u7528 try\/except \u63a5\u4f4f\u7ed3\u675f\u4fe1\u53f7<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums = &#91;1, 2, 3]\nit = iter(nums)\nwhile True:\n    try:\n        value = next(it)\n    except StopIteration:\n        print(\"\u53d6\u5b8c\u4e86\uff0c\u5faa\u73af\u7ed3\u675f\")\n        break\n    print(\"\u53d6\u5230\uff1a\", value)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u53d6\u5230\uff1a 1\n\u53d6\u5230\uff1a 2\n\u53d6\u5230\uff1a 3\n\u53d6\u5b8c\u4e86\uff0c\u5faa\u73af\u7ed3\u675f\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>iter(nums)<\/code> \u8c03\u7528\u5217\u8868\u7684 <code>__iter__<\/code> \u62ff\u5230\u8fed\u4ee3\u5668\uff1b<code>next(it)<\/code> \u8c03\u7528\u8fed\u4ee3\u5668\u7684 <code>__next__<\/code> \u53d6\u503c\u3002<strong>\u8fd9\u6bb5\u4ee3\u7801\u548c <code>for x in nums:<\/code> \u505a\u7684\u4e8b\u4e00\u6a21\u4e00\u6837<\/strong>\u2014\u2014for \u7684\u5b8c\u6574\u5c55\u5f00\u5c31\u662f\uff1a\u8c03 <code>iter<\/code>\u3001\u5faa\u73af <code>next<\/code>\u3001\u6355\u83b7 <code>StopIteration<\/code>\u3001break\u3001\u5904\u7406\u5143\u7d20\u3002<strong>\u7406\u89e3\u8fd9\u4e2a\u5c55\u5f00\uff0c\u4f60\u5c31\u770b\u7a7f\u4e86\u6240\u6709\u5faa\u73af<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u81ea\u5df1\u5199\u4e00\u4e2a\u300c\u80fd\u88ab for \u5faa\u73af\u904d\u5386\u300d\u7684\u7c7b\uff08\u8fed\u4ee3\u5668\u534f\u8bae\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CountDown:\n    \"\"\"\u4ece start \u5012\u6570\u5230 1 \u7684\u8fed\u4ee3\u5668\"\"\"\n\n    def __init__(self, start):\n        self.current = start\n\n    def __iter__(self):\n        return self          # \u8fd4\u56de\u81ea\u5df1\u5373\u53ef\uff08\u81ea\u5df1\u5c31\u662f\u8fed\u4ee3\u5668\uff09\n\n    def __next__(self):\n        if self.current &lt; 1:\n            raise StopIteration   # \u6ca1\u6709\u5143\u7d20\u4e86\uff0c\u629b\u5f02\u5e38\u544a\u8bc9 for \u5faa\u73af\"\u7ed3\u675f\"\n        value = self.current\n        self.current -= 1\n        return value\n\nfor n in CountDown(3):\n    print(n)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3\n2\n1\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5bf9\u8fed\u4ee3\u5668\u91cd\u590d\u4f7f\u7528\uff1a<code>list(it)<\/code> \u540e\u8fd8\u60f3\u518d\u904d\u5386 it \u2192 \u95ee\u9898\uff1a\u7b2c\u4e8c\u6b21\u904d\u5386\u7ed3\u679c\u4e3a\u7a7a\uff08\u8fed\u4ee3\u5668\u662f\u4e00\u6b21\u6027\u7684\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9700\u8981\u91cd\u590d\u904d\u5386\u5c31\u91cd\u65b0 <code>iter(\u539f\u59cb\u5bf9\u8c61)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53ea\u5b9e\u73b0\u4e86 <code>__next__<\/code> \u6ca1\u5b9e\u73b0 <code>__iter__<\/code>\uff0c\u7136\u540e <code>for<\/code> \u904d\u5386 \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError: 'X' object is not iterable<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e24\u4e2a\u65b9\u6cd5\u90fd\u5b9e\u73b0\uff08\u53ef\u8fed\u4ee3 + \u8fed\u4ee3\u5668\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aiter \u62ff\u5de5\u5177\uff0cnext \u53d6\u4e00\u4e2a\uff0cStopIteration \u662f\u53e5\u53f7\uff1b\u8fed\u4ee3\u5668\u4e00\u6b21\u6027\uff0c\u7528\u5b8c\u5f97\u91cd\u6765\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 \u60f0\u6027\u6c42\u503c \u2014\u2014 \u751f\u6210\u5668\u4e3a\u4ec0\u4e48\u7701\u5185\u5b58<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u60f0\u6027\u6c42\u503c<\/strong>\uff08lazy evaluation\uff09\u7684\u610f\u601d\u662f\u300c<strong>\u7528\u5230\u624d\u7b97\uff0c\u4e0d\u63d0\u524d\u5168\u9020\u51fa\u6765<\/strong>\u300d\u3002\u5217\u8868\u63a8\u5bfc\u5f0f <code>[i for i in range(\u767e\u4e07)]<\/code> \u4e00\u6b21\u6027\u628a\u767e\u4e07\u4e2a\u6570\u5168\u653e\u5185\u5b58\uff1b\u751f\u6210\u5668\u8868\u8fbe\u5f0f <code>(i for i in range(\u767e\u4e07))<\/code> \u53ea\u5360\u4e00\u5c0f\u5757\u5185\u5b58\uff0c\u9700\u8981\u54ea\u4e2a\u73b0\u7b97\u54ea\u4e2a\u2014\u2014\u5904\u7406\u5927\u6587\u4ef6\u3001\u5927\u65e5\u5fd7\u3001\u65e0\u9650\u5e8f\u5217\u65f6\uff0c\u8fd9\u662f\u5185\u5b58\u7684\u300c\u6551\u547d\u7a3b\u8349\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u5217\u8868\uff1a\u521b\u5efa\u65f6\u5c31\u904d\u5386\u5b8c\u6574\u4e2a\u5e8f\u5217\uff0c\u628a\u6240\u6709\u503c\u5b58\u8fdb\u8fde\u7eed\u5185\u5b58\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff1a\u5b83\u53ea\u8bb0\u4f4f\u300c\u89c4\u5219\u300d\uff08\u600e\u4e48\u7b97\u4e0b\u4e00\u4e2a\uff09\uff0c<strong>\u5185\u5b58\u91cc\u6ca1\u6709\u4efb\u4f55\u503c<\/strong>\uff1b\u6bcf\u6b21 <code>next()<\/code> \u624d\u73b0\u573a\u7b97\u4e00\u4e2a\u51fa\u6765\uff0c\u7b97\u5b8c\u5373\u5f03\u3002\u65f6\u95f4\u4e0a\u751f\u6210\u5668\u5e76\u4e0d\u66f4\u5feb\uff08\u751a\u81f3\u7565\u6162\uff0c\u7701\u7684\u662f\u5185\u5b58\uff09\uff0c\u5b83\u7684\u4ef7\u503c\u662f<strong>\u8ba9\u300c\u65e0\u9650\u300d\u548c\u300c\u8d85\u5927\u300d\u6210\u4e3a\u53ef\u80fd<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5217\u8868\u63a8\u5bfc\u5f0f\u50cf<strong>\u8d85\u5e02\u63d0\u524d\u628a\u4e00\u5e74\u7684\u5927\u7c73\u5168\u642c\u8fdb\u4ed3\u5e93<\/strong>\uff1a\u53d6\u7528\u65b9\u4fbf\u4f46\u4ed3\u5e93\u5f97\u591f\u5927\uff1b\u751f\u6210\u5668\u50cf<strong>\u5927\u7c73\u968f\u5403\u968f\u4e70<\/strong>\uff1a\u5bb6\u91cc\u53ea\u5b58\u4e00\u5c0f\u888b\uff0c\u9700\u8981\u65f6\u53bb\u7c73\u5e97\u73b0\u4e70\u4e00\u888b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u5217\u8868 vs \u751f\u6210\u5668\uff0c\u5185\u5b58\u5360\u7528\u5bf9\u6bd4<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nbig_list = &#91;i for i in range(1000000)]\nbig_gen = (i for i in range(1000000))\nprint(sys.getsizeof(big_list))   # \u5217\u8868\uff08\u793a\u4f8b\u8f93\u51fa\uff1a8448728\uff09\nprint(sys.getsizeof(big_gen))    # \u751f\u6210\u5668\uff08\u793a\u4f8b\u8f93\u51fa\uff1a200\uff09\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08\u793a\u4f8b\u8f93\u51fa\uff0c\u4e0d\u540c\u673a\u5668\u6570\u503c\u4e0d\u540c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>8448728\n200\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e24\u4e2a\u5bf9\u8c61\u300c\u5305\u542b\u300d\u7684\u6570\u636e\u4e00\u6837\u591a\uff08\u90fd\u662f 100 \u4e07\u4e2a\u6570\uff09\uff0c\u4f46\u5185\u5b58\u5dee\u4e86 <strong>4 \u4e07\u500d<\/strong>\uff1a\u5217\u8868\u628a 100 \u4e07\u4e2a\u6574\u6570\u5168\u5b58\u4e0b\uff08\u7ea6 8MB\uff09\uff0c\u751f\u6210\u5668\u53ea\u5b58\u89c4\u5219\u548c\u72b6\u6001\uff08\u7ea6 200 \u5b57\u8282\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u751f\u6210\u5668\u300c\u73b0\u7b97\u73b0\u7ed9\u300d\u2014\u2014\u6570\u636e\u662f\u73b0\u573a\u9020\u51fa\u6765\u7684\uff1b\u8fd8\u80fd\u5904\u7406\u300c\u65e0\u9650\u5e8f\u5217\u300d<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gen = (x * x for x in range(5))\nprint(next(gen))     # \u73b0\u573a\u7b97\u51fa 0\nprint(next(gen))     # \u73b0\u573a\u7b97\u51fa 1\nprint(list(gen))     # \u628a\u5269\u4e0b\u7684 4\u30019\u300116 \u5168\u90e8\u53d6\u5b8c\n\ndef all_evens():\n    n = 0\n    while True:\n        yield n\n        n += 2\n\ngen2 = all_evens()\nfor _ in range(3):\n    print(next(gen2))        # 0\u30012\u30014\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n&#91;4, 9, 16]\n0\n2\n4\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>all_evens<\/code> \u91cc\u6709 <code>while True<\/code>\uff08\u65e0\u9650\u5faa\u73af\uff09\uff0c\u8c03\u7528\u5b83<strong>\u4e0d\u4f1a\u5361\u6b7b<\/strong>\u2014\u2014\u6bcf\u6b21 <code>next<\/code> \u624d\u6267\u884c\u5230 <code>yield n<\/code> \u6682\u505c\uff0c\u6240\u4ee5\u300c\u65e0\u9650\u5e8f\u5217\u300d\u5b8c\u5168\u5408\u6cd5\uff1a<strong>\u8981\u591a\u5c11\u7ed9\u591a\u5c11\uff0c\u4e0d\u8981\u5c31\u4e0d\u7b97<\/strong>\uff1b\u82e5\u5199\u5217\u8868\u63a8\u5bfc\u5f0f <code>[n for n in ...\u65e0\u9650...]<\/code>\uff0c\u5185\u5b58\u4f1a\u76f4\u63a5\u7206\u70b8\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u628a\u751f\u6210\u5668\u5f53\u5217\u8868\u7528\uff1a<code>gen[0]<\/code>\u3001<code>len(gen)<\/code> \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError: 'generator' object is not subscriptable<\/code>\uff08\u751f\u6210\u5668\u4e0d\u652f\u6301\u4e0b\u6807\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9700\u8981\u4e0b\u6807\u5148 <code>list(gen)<\/code>\uff08\u4f46\u4f1a\u5931\u53bb\u7701\u5185\u5b58\u4f18\u52bf\uff09\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>sum(gen)<\/code> \u4e4b\u540e\u518d\u7528 gen \u2192 \u95ee\u9898\uff1a\u7ed3\u679c\u662f 0\u2014\u2014\u751f\u6210\u5668\u5df2\u88ab\u53d6\u7a7a \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9700\u8981\u591a\u6b21\u4f7f\u7528\u5c31\u5b58\u6210\u5217\u8868\uff0c\u6216\u7528 <code>itertools.tee<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u60f0\u6027\u6c42\u503c\u73b0\u7b97\u73b0\u5356\uff0c\u5185\u5b58\u53ea\u5b58\u89c4\u5219\uff1b\u65e0\u9650\u5e8f\u5217\u4e5f\u4e0d\u6015\uff0c\u8981\u591a\u5c11\u7ed9\u591a\u5c11\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 yield \u7684\u6682\u505c\u4e0e\u6062\u590d \u2014\u2014 \u751f\u6210\u5668\u7684\u6267\u884c\u72b6\u6001\u673a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>yield<\/code> \u662f\u751f\u6210\u5668\u51fd\u6570\u91cc\u7684\u300c\u6682\u505c\u952e\u300d\uff1a\u51fd\u6570\u6267\u884c\u5230 <code>yield \u503c<\/code> \u65f6\uff0c\u628a\u503c\u4ea4\u7ed9\u8c03\u7528\u65b9\uff0c\u7136\u540e<strong>\u539f\u5730\u51bb\u7ed3<\/strong>\uff08\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u3001\u6267\u884c\u4f4d\u7f6e\u90fd\u4fdd\u7559\uff09\uff1b\u4e0b\u6b21 <code>next()<\/code> \u65f6\u4ece\u6682\u505c\u5904<strong>\u7ee7\u7eed\u6267\u884c<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u666e\u901a\u51fd\u6570\u8c03\u7528\u65f6\uff1a\u8fdb\u6808\u3001\u6267\u884c\u3001\u8fd4\u56de\u3001\u51fa\u6808\uff0c\u5c40\u90e8\u53d8\u91cf\u5168\u90e8\u9500\u6bc1\u3002\u751f\u6210\u5668\u51fd\u6570\u4e0d\u540c\uff1a\u5b83\u88ab\u7f16\u8bd1\u6210\u4e00\u4e2a<strong>\u72b6\u6001\u673a<\/strong>\uff08Python \u5185\u90e8\u7528\u4e00\u4e2a\u5bf9\u8c61\u8bb0\u5f55\u6267\u884c\u5230\u54ea\u4e00\u884c\u3001\u5404\u5c40\u90e8\u53d8\u91cf\u7684\u503c\uff09\u3002<code>next(gen)<\/code> \u89e6\u53d1\u72b6\u6001\u673a\u300c\u4ece\u4e0a\u6b21\u4f4d\u7f6e\u8fd0\u884c\u5230\u4e0b\u4e00\u4e2a <code>yield<\/code>\u300d\uff0c\u8fd4\u56de\u503c\u5e76\u6682\u505c\u3002\u6240\u4ee5\u300c\u51fd\u6570\u4e2d\u95f4\u80fd\u505c\u4e0b\u3001\u8fd8\u80fd\u4ece\u539f\u5730\u63a5\u7740\u8d70\u300d\u2014\u2014\u8fd9\u662f\u666e\u901a\u51fd\u6570\u505a\u4e0d\u5230\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>yield<\/code> \u50cf<strong>\u8fde\u7eed\u5267\u7684\u300c\u672c\u96c6\u5b8c\u300d<\/strong>\uff1a\u6bcf\u96c6\uff08\u6bcf\u6b21 next\uff09\u6f14\u5230\u5173\u952e\u5904\u5c31\u505c\uff0c\u4f46\u5267\u60c5\uff08\u72b6\u6001\uff09\u5168\u90e8\u4fdd\u7559\uff1b\u4e0b\u96c6\u4ece\u4e0a\u6b21\u505c\u7684\u5730\u65b9\u63a5\u7740\u6f14\u2014\u2014\u666e\u901a\u51fd\u6570\u5219\u662f\u300c\u4e00\u573a\u7535\u5f71\u300d\uff1a\u4e00\u6b21\u653e\u5b8c\uff0c\u6563\u573a\u6e05\u573a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u5728 yield \u524d\u540e\u653e print\uff0c\u89c2\u5bdf\u300c\u6682\u505c-\u6062\u590d\u300d\u7684\u7cbe\u786e\u987a\u5e8f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def counter():\n    print(\"\u51fd\u6570\u5f00\u59cb\u6267\u884c\")\n    yield 1\n    print(\"\u7b2c\u4e00\u6b21\u6062\u590d\uff0c\u7ee7\u7eed\u5f80\u540e\")\n    yield 2\n    print(\"\u7b2c\u4e8c\u6b21\u6062\u590d\uff0c\u7ee7\u7eed\u5f80\u540e\")\n    yield 3\n\ngen = counter()            # \u6ce8\u610f\uff1a\u6b64\u65f6\u51fd\u6570\u4f53\u4e00\u884c\u90fd\u6ca1\u6267\u884c\uff01\nprint(\"\u62ff\u5230\u751f\u6210\u5668\u5bf9\u8c61\")\nprint(next(gen))           # \u6267\u884c\u5230\u7b2c\u4e00\u4e2a yield\uff0c\u6682\u505c\nprint(next(gen))           # \u4ece\u4e0a\u6b21\u6682\u505c\u5904\u7ee7\u7eed\uff0c\u8dd1\u5230\u7b2c\u4e8c\u4e2a yield\nprint(next(gen))           # \u518d\u7ee7\u7eed\uff0c\u8dd1\u5230\u7b2c\u4e09\u4e2a yield\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u62ff\u5230\u751f\u6210\u5668\u5bf9\u8c61\n\u51fd\u6570\u5f00\u59cb\u6267\u884c\n1\n\u7b2c\u4e00\u6b21\u6062\u590d\uff0c\u7ee7\u7eed\u5f80\u540e\n2\n\u7b2c\u4e8c\u6b21\u6062\u590d\uff0c\u7ee7\u7eed\u5f80\u540e\n3\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u952e\u770b\u6253\u5370\u987a\u5e8f\u2014\u2014<code>counter()<\/code> <strong>\u4e0d\u6267\u884c\u51fd\u6570\u4f53<\/strong>\uff0c\u6240\u4ee5\u300c\u62ff\u5230\u751f\u6210\u5668\u5bf9\u8c61\u300d\u5148\u8f93\u51fa\uff1b\u7b2c\u4e00\u6b21 <code>next<\/code> \u624d\u4ece\u51fd\u6570\u5f00\u5934\u6267\u884c\u5230\u7b2c\u4e00\u4e2a <code>yield 1<\/code> \u5e76\u6682\u505c\uff1b\u7b2c\u4e8c\u6b21 <code>next<\/code> \u4ece\u6682\u505c\u5904\u7ee7\u7eed\u2026\u2026\u4e09\u6b21 next \u4e09\u6b21\u300c\u8dd1-\u505c\u300d\uff0c\u72b6\u6001\u4e0d\u4e22\u3002\u5bf9\u6bd4\u666e\u901a\u51fd\u6570\uff1a<code>def f(): return 42<\/code> \u4e00\u8c03\u7528\u7acb\u523b\u6267\u884c\uff0c\u800c <code>def g(): yield 42<\/code> \u8c03\u7528\u53ea\u8fd4\u56de\u751f\u6210\u5668\u5bf9\u8c61\u3001\u51fd\u6570\u4f53\u4e00\u884c\u6ca1\u8dd1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u751f\u6210\u5668\u7b80\u5199\uff1a\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong>\u2014\u2014\u628a\u5217\u8868\u63a8\u5bfc\u5f0f\u7684 <code>[]<\/code> \u6362\u6210 <code>()<\/code> \u5c31\u662f\u60f0\u6027\u7684\uff1a<code>gen = (x * x for x in range(5))<\/code>\uff0c\u7528 <code>sum(gen)<\/code> \u6c42\u548c\u4e0d\u5360\u989d\u5916\u5185\u5b58\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u51fd\u6570\u91cc\u5199\u4e86 <code>yield<\/code> \u5374\u5f53\u666e\u901a\u51fd\u6570\u7528\uff1a<code>counter()<\/code> \u60f3\u76f4\u63a5\u62ff\u7ed3\u679c \u2192 \u95ee\u9898\uff1a\u62ff\u5230\u7684\u662f\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u4e0d\u662f\u7b2c\u4e00\u4e2a\u503c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>next(gen)<\/code> \u6216 <code>for x in gen<\/code> \u53d6\u503c\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5728\u5faa\u73af\u5916\u5199 yield \u540e\u4e0d\u518d\u7ee7\u7eed \u2192 \u95ee\u9898\uff1a\u751f\u6210\u5668\u53ea\u4ea7\u51fa\u4e00\u4e2a\u503c\uff0c\u7b2c\u4e8c\u6b21 next \u629b StopIteration \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u60f3\u4ea7\u591a\u4e2a\u503c\uff0cyield \u5199\u5728\u5faa\u73af\u91cc\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1ayield \u662f\u6682\u505c\u952e\uff0c\u72b6\u6001\u5168\u4fdd\u7559\uff1bnext \u4e00\u6309\u63a5\u7740\u6f14\uff0c\u6f14\u5b8c\u629b\u53e5\u53f7\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 @staticmethod \u4e0e @classmethod \u2014\u2014 \u4e09\u79cd\u65b9\u6cd5\u7684\u533a\u522b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u666e\u901a\u65b9\u6cd5\u63a5\u6536 <code>self<\/code>\uff08\u5b9e\u4f8b\uff09\uff1b\u7c7b\u65b9\u6cd5\u63a5\u6536 <code>cls<\/code>\uff08\u7c7b\u672c\u8eab\uff09\uff1b\u9759\u6001\u65b9\u6cd5\u4e24\u4e2a\u90fd\u4e0d\u63a5\u6536\u3002\u533a\u522b\u51b3\u5b9a\u4e86\u300c\u8c01\u6709\u8d44\u683c\u8c03\u7528\u3001\u80fd\u8bbf\u95ee\u4ec0\u4e48\u300d\uff1a\u666e\u901a\u65b9\u6cd5\u64cd\u4f5c\u5b9e\u4f8b\u6570\u636e\uff0c\u7c7b\u65b9\u6cd5\u9002\u5408\u300c\u5de5\u5382\u65b9\u6cd5\u300d\uff08\u6362\u4e00\u79cd\u65b9\u5f0f\u521b\u5efa\u5bf9\u8c61\uff09\u548c\u8bbf\u95ee\u7c7b\u5c5e\u6027\uff0c\u9759\u6001\u65b9\u6cd5\u9002\u5408\u300c\u903b\u8f91\u4e0a\u5c5e\u4e8e\u8fd9\u4e2a\u7c7b\u3001\u4f46\u4e0d\u9700\u8981\u4efb\u4f55\u5bf9\u8c61\u6570\u636e\u300d\u7684\u5de5\u5177\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e09\u79cd\u65b9\u6cd5\u7684\u672c\u8d28\u5dee\u5f02\u5728<strong>\u7ed1\u5b9a<\/strong>\uff08binding\uff09\uff1a\u666e\u901a\u65b9\u6cd5\u7ed1\u5b9a\u5230\u5b9e\u4f8b\u2014\u2014<code>obj.method()<\/code> \u81ea\u52a8\u628a obj \u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff1b\u7c7b\u65b9\u6cd5\u7ed1\u5b9a\u5230\u7c7b\u2014\u2014<code>cls.method()<\/code> \u81ea\u52a8\u628a\u7c7b\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u4e14\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u65f6\uff0c\u4f20\u7684\u4e5f\u662f\u300c\u5b9e\u4f8b\u7684\u7c7b\u300d\uff0c\u4e0d\u662f\u5b9e\u4f8b\u672c\u8eab\uff09\uff1b\u9759\u6001\u65b9\u6cd5<strong>\u4e0d\u7ed1\u5b9a<\/strong>\u2014\u2014\u5c31\u662f\u653e\u5728\u7c7b\u91cc\u7684\u666e\u901a\u51fd\u6570\uff0c\u600e\u4e48\u8c03\u7528\u90fd\u4e0d\u6ce8\u5165\u53c2\u6570\u3002<code>@classmethod<\/code> \/ <code>@staticmethod<\/code> \u5e72\u7684\u4e8b\uff0c\u5c31\u662f\u6539\u53d8\u8fd9\u79cd\u7ed1\u5b9a\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e09\u79cd\u65b9\u6cd5\u50cf<strong>\u5458\u5de5\u529e\u4e1a\u52a1 vs \u516c\u53f8\u603b\u673a vs \u524d\u53f0\u5f20\u8d34\u7684\u670d\u52a1\u8bf4\u660e<\/strong>\uff1a\u5458\u5de5\u8981\u5e26\u5de5\u724c\uff08self\uff09\u64cd\u4f5c\u81ea\u5df1\u7684\u4e1a\u52a1\u6570\u636e\uff1b\u6253\u603b\u673a\uff08cls\uff09\u80fd\u529e\u300c\u516c\u53f8\u7ea7\u300d\u7684\u4e8b\uff1b\u670d\u52a1\u8bf4\u660e\u4e0d\u9700\u8981\u8eab\u4efd\uff0c\u8c01\u6765\u4e86\u90fd\u80fd\u7167\u7740\u505a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u4e09\u79cd\u65b9\u6cd5\u7684\u7b7e\u540d\u4e0e\u8c03\u7528\u65b9\u5f0f\u5bf9\u6bd4<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Tool:\n    def normal(self):\n        return \"\u666e\u901a\u65b9\u6cd5\uff1a\u6211\u662f\", self\n\n    @classmethod\n    def create(cls):\n        return cls()          # cls \u5c31\u662f Tool \u7c7b\u672c\u8eab\uff0c\u8c03\u7528\u5b83\u4f1a\u521b\u5efa\u5b9e\u4f8b\n\n    @staticmethod\n    def add(a, b):\n        return a + b\n\nt = Tool()\nprint(t.normal())             # \u666e\u901a\u65b9\u6cd5\uff1a\u5fc5\u987b\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\nprint(Tool.create())          # \u7c7b\u65b9\u6cd5\uff1a\u7c7b\u548c\u5b9e\u4f8b\u90fd\u80fd\u8c03\u7528\nprint(Tool.add(1, 2))         # \u9759\u6001\u65b9\u6cd5\uff1a\u4e0d\u63a5\u6536 self\/cls\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u666e\u901a\u65b9\u6cd5\uff1a\u6211\u662f &lt;__main__.Tool object at 0x...&gt;\uff08\u793a\u4f8b\u8f93\u51fa\uff09\n&lt;__main__.Tool object at 0x...&gt;\uff08\u793a\u4f8b\u8f93\u51fa\uff09\n3\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>t.normal()<\/code> \u91cc self \u81ea\u52a8\u7ed1\u5b9a\u4e3a t\uff1b<code>Tool.create()<\/code> \u91cc cls \u81ea\u52a8\u7ed1\u5b9a\u4e3a Tool\uff0c<code>cls()<\/code> \u5c31\u662f\u5728\u300c\u7528\u7c7b\u672c\u8eab\u521b\u5efa\u5b9e\u4f8b\u300d\uff1b<code>Tool.add(1, 2)<\/code> \u76f4\u63a5\u8c03\u7528\uff0c\u4e0d\u6ce8\u5165\u4efb\u4f55\u53c2\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>classmethod \u5de5\u5382\u65b9\u6cd5\u2014\u2014\u6700\u7ecf\u5178\u7684\u7528\u9014<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee:\n    def __init__(self, name, salary):\n        self.name = name\n        self.salary = salary\n\n    @classmethod\n    def from_string(cls, s):\n        name, salary = s.split(\",\")\n        return cls(name, int(salary))\n\n    def __str__(self):\n        return f\"{self.name}: {self.salary}\u5143\"\n\ne = Employee.from_string(\"\u5f20\u4e09,8000\")\nprint(e)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f20\u4e09: 8000\u5143\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>from_string<\/code> \u662f\u300c\u5de5\u5382\u65b9\u6cd5\u300d\u2014\u2014\u8f93\u5165\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u8f93\u51fa Employee \u5b9e\u4f8b\u3002\u5173\u952e\u5728 <code>return cls(...)<\/code>\uff1a<strong>cls \u662f\u7c7b\u672c\u8eab<\/strong>\uff0c\u5c06\u6765\u6709\u4eba\u7ee7\u627f Employee \u5199\u5b50\u7c7b\uff0c<code>\u5b50\u7c7b.from_string(...)<\/code> \u8fd4\u56de\u7684\u4e5f\u662f\u5b50\u7c7b\u5b9e\u4f8b\uff0c\u6240\u4ee5\u7528 cls \u800c\u4e0d\u662f\u5199\u6b7b Employee\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u9759\u6001\u65b9\u6cd5\u91cc\u5199 <code>self.xxx<\/code> \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError<\/code>\uff08\u9759\u6001\u65b9\u6cd5\u4e0d\u63a5\u6536 self\uff0cself \u672a\u5b9a\u4e49\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9759\u6001\u65b9\u6cd5\u53ea\u7528\u53c2\u6570\uff0c\u4e0d\u78b0\u5b9e\u4f8b\u5c5e\u6027\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7c7b\u65b9\u6cd5\u91cc\u5199 <code>self<\/code> \u2192 \u95ee\u9898\uff1a\u62a5 <code>NameError<\/code> \u6216\u53c2\u6570\u9519\uff08\u7b2c\u4e00\u4e2a\u53c2\u6570\u7ea6\u5b9a\u53eb cls\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7c7b\u65b9\u6cd5\u7b2c\u4e00\u4e2a\u53c2\u6570\u5199 <code>cls<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5de5\u5382\u65b9\u6cd5\u91cc\u5199\u6b7b\u7c7b\u540d\uff1a<code>return Employee(...)<\/code> \u2192 \u95ee\u9898\uff1a\u5b50\u7c7b\u7ee7\u627f\u540e\u8fd4\u56de\u7684\u4ecd\u662f\u7236\u7c7b\u5b9e\u4f8b\uff0c\u7ed1\u5b9a\u9519\u8bef \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>return cls(...)<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u666e\u901a\u5e26 self \u7ed1\u5b9e\u4f8b\uff0c\u7c7b\u65b9\u6cd5\u5e26 cls \u7ed1\u7c7b\uff0c\u9759\u6001\u65b9\u6cd5\u4e0d\u7ed1\u5b9a\uff1b\u5de5\u5382\u65b9\u6cd5\u7528 cls\uff0c\u7ee7\u627f\u4e0d\u7ffb\u8f66\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u88c5\u9970\u5668\u539f\u7406 \u2014\u2014 \u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff0c\u5305\u88c5\u5373\u589e\u5f3a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u88c5\u9970\u5668<\/strong>\uff08decorator\uff09\u662f\u300c<strong>\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u3001\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\u7684\u51fd\u6570<\/strong>\u300d\u3002<code>@log<\/code> \u5199\u5728\u51fd\u6570\u4e0a\u65b9\uff0c\u7b49\u4ef7\u4e8e <code>add = log(add)<\/code>\u2014\u2014\u628a\u539f\u51fd\u6570\u300c\u5305\u88c5\u300d\u8fdb\u4e00\u4e2a\u589e\u5f3a\u7248\u3002\u65e5\u5fd7\u3001\u8ba1\u65f6\u3001\u6743\u9650\u3001\u7f13\u5b58\u8fd9\u7c7b\u300c\u6a2a\u5207\u5173\u6ce8\u70b9\u300d\uff08\u4e0e\u4e1a\u52a1\u65e0\u5173\u3001\u5374\u5230\u5904\u90fd\u8981\uff09\u7528\u88c5\u9970\u5668\u5199\u4e00\u6b21\u3001\u5230\u5904\u590d\u7528\uff0c\u539f\u51fd\u6570\u4e00\u884c\u4e0d\u6539\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u51b3\u6761\u4ef6\u662f\u300c\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\u300d\uff1a\u51fd\u6570\u53ef\u4ee5\u50cf\u53d8\u91cf\u4e00\u6837\u88ab\u4f20\u9012\u3001\u88ab\u8fd4\u56de\u3002<code>@decorator<\/code> \u53ea\u662f\u8bed\u6cd5\u7cd6\uff1aPython \u5728\u5b9a\u4e49\u5b8c\u51fd\u6570\u540e\uff0c\u81ea\u52a8\u6267\u884c <code>func = decorator(func)<\/code> \u5e76<strong>\u7528\u8fd4\u56de\u503c\u66ff\u6362\u539f\u540d\u5b57<\/strong>\u3002\u6240\u4ee5\u88c5\u9970\u5668\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u300c\u53ef\u8c03\u7528\u7684\u4e1c\u897f\u300d\uff08\u901a\u5e38\u662f\u5185\u90e8\u5b9a\u4e49\u7684 wrapper \u51fd\u6570\uff09\uff0cwrapper \u91cc <code>*args, **kwargs<\/code> \u662f\u4e3a\u4e86\u628a\u539f\u51fd\u6570\u7684\u4efb\u610f\u53c2\u6570\u539f\u6837\u8f6c\u53d1\u3002<strong>wrapper \u5fc5\u987b return \u539f\u51fd\u6570\u7684\u7ed3\u679c<\/strong>\uff0c\u5426\u5219\u88ab\u88c5\u9970\u51fd\u6570\u8c03\u7528\u540e\u7684\u8fd4\u56de\u503c\u53d8\u6210 None\uff08\u6700\u5e38\u89c1\u7684\u5751\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u88c5\u9970\u5668\u50cf<strong>\u624b\u673a\u58f3<\/strong>\uff1a\u624b\u673a\uff08\u539f\u51fd\u6570\uff09\u529f\u80fd\u4e0d\u53d8\uff0c\u5957\u4e0a\u58f3\u5c31\u591a\u4e86\u9632\u6454\uff08\u65e5\u5fd7\uff09\u3001\u652f\u67b6\uff08\u8ba1\u65f6\uff09\u7b49\u80fd\u529b\uff1b\u58f3\u4e0d\u6539\u53d8\u624b\u673a\u5185\u90e8\u7535\u8def\uff08\u4e0d\u6539\u539f\u51fd\u6570\u4ee3\u7801\uff09\uff0c\u60f3\u6362\u58f3\u968f\u65f6\u6362\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\u2014\u2014\u51fd\u6570\u80fd\u4f5c\u4e3a\u8fd4\u56de\u503c\uff08\u300c\u52a0\u6cd5\u5668\u5de5\u5382\u300d\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def make_adder(n):\n    def adder(x):            # \u5728\u51fd\u6570\u5185\u90e8\u518d\u5b9a\u4e49\u51fd\u6570\n        return x + n\n    return adder             # \u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\n\nadd_10 = make_adder(10)\nprint(add_10(5))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>15\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u8fd8\u80fd\u5f53\u53c2\u6570\u4f20\u2014\u2014<strong>\u51fd\u6570\u80fd\u50cf\u6570\u636e\u4e00\u6837\u6d41\u8f6c<\/strong>\uff0c\u8fd9\u662f\u88c5\u9970\u5668\u80fd\u6210\u7acb\u7684\u5730\u57fa\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>@ \u8bed\u6cd5\u7cd6\u7684\u771f\u76f8\u2014\u2014\u88c5\u9970\u5668\u5c31\u662f\u300c\u624b\u52a8\u5305\u88c5\u300d\u7684\u7b80\u5199<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def shout(func):\n    def wrapper():\n        print(\"\uff01\uff01\uff01\")\n        func()\n        print(\"\uff01\uff01\uff01\")\n    return wrapper\n\n@shout                   # \u8bed\u6cd5\u7cd6\uff1a\u7b49\u4ef7\u4e8e bye = shout(bye)\ndef bye():\n    print(\"\u518d\u89c1\")\n\nbye()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff01\uff01\uff01\n\u518d\u89c1\n\uff01\uff01\uff01\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>@shout<\/code> \u5199\u5728 def \u4e0a\u9762 = \u5b9a\u4e49\u5b8c\u7acb\u523b\u6267\u884c <code>bye = shout(bye)<\/code>\u2014\u2014bye \u8fd9\u4e2a\u540d\u5b57\u4ece\u6b64\u6307\u5411 wrapper\uff08\u91cc\u9762\u300c\u5305\u88f9\u300d\u7740\u539f\u51fd\u6570\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>wrapper \u5fd8\u8bb0 return \u7684\u5751\u2014\u2014\u8fd4\u56de None \u7684\u51fd\u6570<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def my_log(func):\n    def wrapper(*args, **kwargs):\n        print(f\"\u8c03\u7528 {func.__name__}\")\n        func(*args, **kwargs)        # \u5fd8\u4e86 return\uff01\n    return wrapper\n\n@my_log\ndef add(a, b):\n    return a + b\n\nprint(add(1, 2))         # \u7ed3\u679c\u662f None \u800c\u4e0d\u662f 3\uff01\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8c03\u7528 add\nNone\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>add(1, 2)<\/code> \u5b9e\u9645\u6267\u884c\u7684\u662f wrapper\uff1a\u6253\u5370\u65e5\u5fd7\u3001\u8c03\u7528\u539f\u51fd\u6570\u7b97\u51fa\u4e86 3\uff0c\u4f46 <strong>wrapper \u6ca1\u6709\u628a 3 \u4f20\u51fa\u53bb<\/strong>\uff08\u6ca1\u6709 return\uff09\uff0c\u6240\u4ee5\u8fd4\u56de\u503c\u662f None\u2014\u2014\u51fd\u6570\u300c\u767d\u7b97\u4e86\u300d\uff0c\u800c\u4e14\u4e0d\u62a5\u9519\u3001\u53ea\u662f\u7ed3\u679c\u4e0d\u5bf9\u3002\u4fee\u590d\uff1a<code>return func(*args, **kwargs)<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4 <strong>\u88c5\u9970\u5668\u5e26\u53c2\u6570\uff08\u5916\u5c42\u518d\u5957\u4e00\u5c42\u51fd\u6570\uff0c\u7528\u6765\u4f20\u914d\u7f6e\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def repeat(times):\n    \"\"\"\u88c5\u9970\u5668\u5de5\u5382\uff1a\u8ba9\u51fd\u6570\u6267\u884c times \u6b21\"\"\"\n    def decorator(func):\n        def wrapper(*args, **kwargs):\n            for _ in range(times):\n                func(*args, **kwargs)\n        return wrapper\n    return decorator\n\n@repeat(3)\ndef hello():\n    print(\"\u4f60\u597d\")\n\nhello()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4f60\u597d\n\u4f60\u597d\n\u4f60\u597d\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1awrapper \u91cc\u5fd8\u4e86 <code>return func(...)<\/code> \u2192 \u95ee\u9898\uff1a\u88ab\u88c5\u9970\u51fd\u6570\u8fd4\u56de None\uff0c\u903b\u8f91\u300c\u9759\u9ed8\u51fa\u9519\u300d \u2192 \u6b63\u786e\u5199\u6cd5\uff1awrapper \u91cc <code>return func(*args, **kwargs)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1awrapper \u53c2\u6570\u5199\u6b7b <code>def wrapper(a, b)<\/code> \u2192 \u95ee\u9898\uff1a\u88c5\u9970\u4e0d\u540c\u53c2\u6570\u7684\u51fd\u6570\u5c31\u62a5\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>*args, **kwargs<\/code> \u901a\u914d\u8f6c\u53d1\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u88c5\u9970\u5668\u76f4\u63a5\u8fd4\u56de\u539f\u51fd\u6570\uff1a<code>return func<\/code> \u2192 \u95ee\u9898\uff1a\u88c5\u9970\u5668\u5f62\u540c\u865a\u8bbe\uff0c\u6ca1\u6709\u4efb\u4f55\u589e\u5f3a \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8fd4\u56de\u5185\u90e8 wrapper\uff08\u65b0\u51fd\u6570\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u4e00\u7b49\u516c\u6c11\u53ef\u4f20\u9012\uff0c\u5305\u88c5\u66ff\u6362\u662f\u8bed\u6cd5\u7cd6\uff1bwrapper \u5fc5\u987b return\uff0c\u5426\u5219\u7ed3\u679c\u53d8 None\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6 @property \u2014\u2014 \u628a\u65b9\u6cd5\u53d8\u6210\u5c5e\u6027<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>@property<\/code> \u53ef\u4ee5\u8ba9 <code>obj.\u5c5e\u6027<\/code> \u8fd9\u79cd\u5199\u6cd5\u80cc\u540e\u6267\u884c\u4e00\u6bb5\u65b9\u6cd5\u903b\u8f91\uff0c\u6700\u5e38\u89c1\u4e24\u4e2a\u573a\u666f\uff1a<strong>\u8ba1\u7b97\u5c5e\u6027<\/strong>\uff08\u4e0d\u5b58\u503c\u3001\u73b0\u7b97\uff09\u548c<strong>\u8d4b\u503c\u6821\u9a8c<\/strong>\uff08\u4e0d\u80fd\u968f\u4fbf\u4e71\u5b58\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u8ba1\u7b97\u5c5e\u6027 + \u8d4b\u503c\u6821\u9a8c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Circle:\n    def __init__(self, radius):\n        self._radius = radius\n\n    @property\n    def area(self):\n        return 3.14 * self._radius ** 2\n\nc = Circle(2)\nprint(c.area)        # \u4e0d\u52a0\u62ec\u53f7\uff0c\u50cf\u5c5e\u6027\u4e00\u6837\u8bbf\u95ee\n\nclass Student:\n    def __init__(self, score=0):\n        self._score = 0\n        self.score = score      # \u8d4b\u503c\u4f1a\u8d70\u4e0b\u9762\u7684 setter\n\n    @property\n    def score(self):\n        return self._score\n\n    @score.setter\n    def score(self, value):\n        if not 0 &lt;= value &lt;= 100:\n            raise ValueError(\"\u5206\u6570\u5fc5\u987b\u5728 0~100 \u4e4b\u95f4\")\n        self._score = value\n\ns = Student(85)\nprint(s.score)\ns.score = 60         # \u5199\uff1a\u8d70 setter\uff0c\u5e26\u6821\u9a8c\nprint(s.score)\n# s.score = 999      # \u53d6\u6d88\u6ce8\u91ca\u4f1a\u629b ValueError\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>12.56\n85\n60\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e3a\u4ec0\u4e48\u7528 @property<\/strong>\uff1a\u5916\u90e8\u4ee3\u7801\u5199 <code>obj.area<\/code> \u5f88\u81ea\u7136\uff0c\u4ee5\u540e\u5185\u90e8\u5b9e\u73b0\u60f3\u6539\uff08\u6bd4\u5982\u6362\u66f4\u7cbe\u786e\u7684\u5706\u5468\u7387\uff09\u4e5f\u4e0d\u5f71\u54cd\u5916\u90e8\u8c03\u7528\u65b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>@property<\/code> \u65b9\u6cd5\u540d\u548c\u5185\u90e8\u5c5e\u6027\u540c\u540d\uff08<code>self.radius<\/code> \u53c8\u5b9a\u4e49 <code>def radius<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u65e0\u9650\u9012\u5f52\uff0c\u62a5 <code>RecursionError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5185\u90e8\u5b58 <code>self._radius<\/code>\uff0c\u5916\u90e8\u5c5e\u6027\u540d\u7528 <code>radius<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aproperty \u53d8\u5c5e\u6027\uff0c\u70b9\u53f7\u5c31\u80fd\u53d6\uff1bsetter \u52a0\u6821\u9a8c\uff0c\u5185\u90e8\u5b58\u4e0b\u5212\u7ebf\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7 \u52a8\u624b\u5b9e\u8df5\uff1a\u6570\u636e\u6d41\u6c34\u7ebf\u5de5\u5177<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bbe\u8ba1\u4e00\u6761\u300c\u6570\u636e\u6d41\u6c34\u7ebf\u300d\uff1a\u6e90\u5934\u751f\u6210\u5668\u4ea7\u751f\u6570\u5b57\uff0c\u7ecf\u8fc7\u300c\u653e\u5927\u300d\u3001\u300c\u8fc7\u6ee4\u300d\u4e24\u4e2a\u52a0\u5de5\u73af\u8282\uff0c\u6700\u540e\u6536\u96c6\u7ed3\u679c\u2014\u2014\u628a\u4eca\u5929\u5b66\u7684\u751f\u6210\u5668\u771f\u6b63\u4e32\u8054\u8d77\u6765\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># pipeline.py \u2014\u2014 \u6570\u636e\u6d41\u6c34\u7ebf\u5de5\u5177\ndef numbers(limit):\n    \"\"\"\u6e90\u5934\uff1a\u9010\u4e2a\u4ea7\u51fa 1~limit\"\"\"\n    for n in range(1, limit + 1):\n        yield n\n\n\ndef multiply(factor):\n    \"\"\"\u52a0\u5de5\u73af\u8282\u5de5\u5382\uff1a\u628a\u4e0a\u6e38\u6bcf\u4e2a\u6570\u4e58\u4ee5 factor\"\"\"\n    def transform(source):\n        for item in source:\n            yield item * factor\n    return transform\n\n\ndef filter_big(threshold):\n    \"\"\"\u52a0\u5de5\u73af\u8282\u5de5\u5382\uff1a\u53ea\u4fdd\u7559\u5927\u4e8e threshold \u7684\u6570\"\"\"\n    def transform(source):\n        for item in source:\n            if item &gt; threshold:\n                yield item\n    return transform\n\n\ndef collect(source):\n    \"\"\"\u672b\u7aef\uff1a\u628a\u6570\u636e\u6536\u8fdb\u5217\u8868\"\"\"\n    return list(source)\n\n\ndef run_pipeline(limit, factor, threshold):\n    source = numbers(limit)\n    source = multiply(factor)(source)     # \u73af\u82821\uff1a\u653e\u5927\n    source = filter_big(threshold)(source)  # \u73af\u82822\uff1a\u8fc7\u6ee4\n    result = collect(source)\n    print(\"\u6700\u7ec8\u7ed3\u679c:\", result)\n\n\nrun_pipeline(10, 3, 20)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6700\u7ec8\u7ed3\u679c: &#91;21, 24, 27, 30]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>1~10<\/code> \u4e58 3 \u5f97\u5230 <code>3,6,...,30<\/code>\uff0c\u518d\u7b5b\u6389 <code>&lt;=20<\/code> \u7684\uff0c\u5c31\u5269\u4e0b <code>21,24,27,30<\/code>\u3002\u6574\u6761\u6d41\u6c34\u7ebf\u6bcf\u4e2a\u73af\u8282\u90fd\u662f\u751f\u6210\u5668\uff0c\u6570\u636e\u4e00\u8def\u300c\u8fb9\u7b97\u8fb9\u6d41\u300d\uff0c\u4e0d\u4f1a\u4e00\u6b21\u6027\u9020\u4e00\u4e2a\u5927\u5217\u8868\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5347\u7ea7\u6311\u6218<\/strong>\uff1a\u518d\u5199\u4e00\u4e2a\u300c\u7d2f\u52a0\u6c42\u548c\u300d\u73af\u8282\uff08\u6bd4\u5982\u628a\u8fc7\u6ee4\u540e\u7684\u6570\u5168\u90e8\u52a0\u8d77\u6765\uff09\uff1b\u8bd5\u8bd5\u628a\u6d41\u6c34\u7ebf\u73af\u8282\u7528\u7c7b\u7ec4\u7ec7\uff0c\u8ba9\u6bcf\u4e2a\u73af\u8282\u662f\u4e00\u4e2a\u5bf9\u8c61\uff1b\u6216\u6309\u77e5\u8bc6\u70b9 5 \u7684\u5199\u6cd5\u7ed9 <code>run_pipeline<\/code> \u5305\u4e00\u4e2a\u8ba1\u65f6\u88c5\u9970\u5668\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fed\u4ee3\u5668\u534f\u8bae\uff1a<code>iter()<\/code> \u62ff\u8fed\u4ee3\u5668\u3001<code>next()<\/code> \u9010\u4e2a\u53d6\u503c\u3001<code>StopIteration<\/code> \u8868\u793a\u7ed3\u675f\uff1b<code>for<\/code> \u5faa\u73af\u5c31\u662f\u8fd9\u4e09\u6b65\u7684\u8bed\u6cd5\u7cd6\uff1b\u8fed\u4ee3\u5668\u662f\u4e00\u6b21\u6027\u7684\uff08\u53d6\u5b8c\u5373\u7a7a\uff09\uff0c\u53ef\u8fed\u4ee3\u5bf9\u8c61 \u2260 \u8fed\u4ee3\u5668\n\u60f0\u6027\u6c42\u503c\uff1a\u751f\u6210\u5668\u300c\u7528\u5230\u624d\u7b97\u300d\uff0c\u5185\u5b58\u5360\u7528\u4e0e\u6570\u636e\u603b\u91cf\u65e0\u5173\uff0c\u80fd\u5904\u7406\u5927\u6587\u4ef6\u4e0e\u65e0\u9650\u5e8f\u5217\n<code>yield<\/code> \u662f\u6682\u505c\u952e\uff1a\u72b6\u6001\u5168\u4fdd\u7559\uff0c<code>next()<\/code> \u4ece\u4e0a\u6b21\u6682\u505c\u5904\u7ee7\u7eed\u6267\u884c\uff1b\u533a\u5206\u300c\u8c03\u7528\u751f\u6210\u5668\u51fd\u6570\u300d\u4e0e\u300c\u6267\u884c\u751f\u6210\u5668\u51fd\u6570\u4f53\u300d\uff08\u524d\u8005\u53ea\u8fd4\u56de\u5bf9\u8c61\uff09\n\u4e09\u79cd\u65b9\u6cd5\uff1a\u666e\u901a\u65b9\u6cd5\uff08self\uff0c\u7ed1\u5b9e\u4f8b\uff09\u3001\u7c7b\u65b9\u6cd5\uff08cls\uff0c\u7ed1\u7c7b\uff0c\u9002\u5408\u5de5\u5382\u65b9\u6cd5\uff09\u3001\u9759\u6001\u65b9\u6cd5\uff08\u4e0d\u7ed1\u5b9a\uff09\n<code>@property<\/code> \u628a\u65b9\u6cd5\u53d8\u6210\u5c5e\u6027\uff1a\u8ba1\u7b97\u5c5e\u6027 + setter \u8d4b\u503c\u6821\u9a8c\n\u88c5\u9970\u5668 = \u63a5\u6536\u51fd\u6570\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\uff0c<code>@x<\/code> \u7b49\u4ef7\u4e8e <code>f = x(f)<\/code>\uff1bwrapper \u5fc5\u987b <code>return func(...)<\/code>\n\u6570\u636e\u6d41\u6c34\u7ebf\u5de5\u5177\u80fd\u6b63\u5e38\u8dd1\u901a\u5168\u90e8\u73af\u8282\uff08\u6e90\u5934\/\u52a0\u5de5\/\u6536\u96c6\u90fd\u662f\u60f0\u6027\u751f\u6210\u5668\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>StopIteration<\/code><\/td><td>\u8fed\u4ee3\u5668\u5df2\u7ecf\u53d6\u5b8c\uff0c\u8fd8\u7ee7\u7eed <code>next()<\/code><\/td><td>\u7528 <code>for<\/code> \u5faa\u73af\u81ea\u52a8\u5904\u7406\u7ed3\u675f\uff0c\u6216\u81ea\u5df1\u6355\u83b7 <code>StopIteration<\/code><\/td><\/tr><tr><td><code>TypeError: 'CountDown' object is not iterable<\/code><\/td><td>\u7c7b\u6ca1\u5b9e\u73b0 <code>__iter__<\/code> \u5c31\u60f3 <code>for<\/code> \u904d\u5386<\/td><td>\u5b9e\u73b0 <code>__iter__<\/code>\uff08\u8fd4\u56de\u8fed\u4ee3\u5668\uff09\u548c <code>__next__<\/code><\/td><\/tr><tr><td><code>TypeError: 'generator' object is not subscriptable<\/code><\/td><td>\u751f\u6210\u5668\u4e0d\u652f\u6301 <code>gen[0]<\/code> \u8fd9\u79cd\u4e0b\u6807\u53d6\u503c<\/td><td>\u5148 <code>list(gen)<\/code> \u8f6c\u6210\u5217\u8868\uff0c\u6216\u6539\u7528\u8fed\u4ee3<\/td><\/tr><tr><td>\u88ab\u88c5\u9970\u51fd\u6570\u8fd4\u56de <code>None<\/code><\/td><td><code>wrapper<\/code> \u91cc\u5fd8\u4e86 <code>return func(*args, **kwargs)<\/code><\/td><td>\u5728 <code>wrapper<\/code> \u91cc\u628a\u7ed3\u679c return \u51fa\u53bb<\/td><\/tr><tr><td><code>AttributeError: 'function' object has no attribute 'xxx'<\/code><\/td><td>\u88c5\u9970\u5668\u628a\u539f\u51fd\u6570\u6362\u6210\u4e86 <code>wrapper<\/code>\uff0c\u4e22\u5931\u4e86\u539f\u51fd\u6570\u4fe1\u606f<\/td><td>\u5728 <code>wrapper<\/code> \u4e0a\u518d\u52a0 <code>@functools.wraps(func)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u8fed\u4ee3\u5668\u534f\u8bae \u2014\u2014 for \u5faa\u73af\u7684\u5e55\u540e\u771f\u76f8 \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u8fed\u4ee3\u5668\uff08iterator\uff09\u662f\u300c\u4e00\u6b21\u53d6\u4e00\u4e2a\u300d\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[17],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev","tag-python-day"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}