{"id":744,"date":"2022-05-13T16:24:30","date_gmt":"2022-05-13T08:24:30","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=744"},"modified":"2024-12-22T14:24:43","modified_gmt":"2024-12-22T06:24:43","slug":"python%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2022\/05\/13\/python%e7%ac%94%e8%ae%b0\/","title":{"rendered":"Python\u7591\u70b9-\u590d\u4e60\u7528"},"content":{"rendered":"\n<p>\u6458\u81ea\u5b66\u4e60\u65f6<a href=\"https:\/\/liaoxuefeng.com\/books\/python\/introduction\/index.html\">https:\/\/liaoxuefeng.com\/books\/python\/introduction\/index.html<\/a><\/p>\n\n\n\n<p class=\"has-large-font-size\">\u4e00\u3001<\/p>\n\n\n\n<p class=\"has-small-font-size\">\u7406\u89e3\u53d8\u91cf\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u8868\u793a\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u5f53\u6211\u4eec\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 'ABC'\n<\/code><\/pre>\n\n\n\n<p>\u65f6\uff0cPython\u89e3\u91ca\u5668\u5e72\u4e86\u4e24\u4ef6\u4e8b\u60c5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a<code>'ABC'<\/code>\u7684\u5b57\u7b26\u4e32\uff1b<\/li>\n\n\n\n<li>\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>a<\/code>\u7684\u53d8\u91cf\uff0c\u5e76\u628a\u5b83<strong>\u6307\u5411<\/strong><code>'ABC'<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u628a\u4e00\u4e2a\u53d8\u91cf<code>a<\/code>\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u53d8\u91cf<code>b<\/code>\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u628a\u53d8\u91cf<code>b<\/code>\u6307\u5411\u53d8\u91cf<code>a<\/code><strong>\u6240\u6307\u5411\u7684<\/strong>\u6570\u636e<\/p>\n\n\n\n<p>a = &#8216;ABC&#8217;<br>b = a<br>a = &#8216;XYZ&#8217;<br>print(b)<\/p>\n\n\n\n<p><code>b<\/code>\u7684\u503c\u662f<code>'ABC'<\/code>\uff0c\u56e0\u4e3a\u6267\u884c<code>a = 'XYZ'<\/code>\uff0c\u89e3\u91ca\u5668\u521b\u5efa\u4e86\u5b57\u7b26\u4e32&#8217;XYZ&#8217;\uff0c\u5e76\u628a<code>a<\/code>\u7684\u6307\u5411\u6539\u4e3a<code>'XYZ'<\/code>\uff0c\u4f46<code>b<\/code>\u5e76\u6ca1\u6709\u66f4\u6539<\/p>\n\n\n\n<p>\u6458\u81ea<a href=\"https:\/\/liaoxuefeng.com\/books\/python\/basic\/data-types\/index.html\">https:\/\/liaoxuefeng.com\/books\/python\/basic\/data-types\/index.html<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u518d\u8bae\u4e0d\u53ef\u53d8\u5bf9\u8c61<\/h3>\n\n\n\n<p>\u4e0a\u9762\u6211\u4eec\u8bb2\u4e86\uff0cstr\u662f\u4e0d\u53d8\u5bf9\u8c61\uff0c\u800clist\u662f\u53ef\u53d8\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u53ef\u53d8\u5bf9\u8c61\uff0c\u6bd4\u5982list\uff0c\u5bf9list\u8fdb\u884c\u64cd\u4f5c\uff0clist\u5185\u90e8\u7684\u5185\u5bb9\u662f\u4f1a\u53d8\u5316\u7684\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; a = &#91;'c', 'b', 'a']\n&gt;&gt;&gt; a.sort()\n&gt;&gt;&gt; a\n&#91;'a', 'b', 'c']\n<\/code><\/pre>\n\n\n\n<p>\u800c\u5bf9\u4e8e\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u6bd4\u5982str\uff0c\u5bf9str\u8fdb\u884c\u64cd\u4f5c\u5462\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; a = 'abc'\n&gt;&gt;&gt; a.replace('a', 'A')\n'Abc'\n&gt;&gt;&gt; a\n'abc'\n<\/code><\/pre>\n\n\n\n<p>\u867d\u7136\u5b57\u7b26\u4e32\u6709\u4e2a<code>replace()<\/code>\u65b9\u6cd5\uff0c\u4e5f\u786e\u5b9e\u53d8\u51fa\u4e86<code>'Abc'<\/code>\uff0c\u4f46\u53d8\u91cf<code>a<\/code>\u6700\u540e\u4ecd\u662f<code>'abc'<\/code>\uff0c\u5e94\u8be5\u600e\u4e48\u7406\u89e3\u5462\uff1f<\/p>\n\n\n\n<p>\u6211\u4eec\u5148\u628a\u4ee3\u7801\u6539\u6210\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; a = 'abc'\n&gt;&gt;&gt; b = a.replace('a', 'A')\n&gt;&gt;&gt; b\n'Abc'\n&gt;&gt;&gt; a\n'abc'\n<\/code><\/pre>\n\n\n\n<p>\u8981\u59cb\u7ec8\u7262\u8bb0\u7684\u662f\uff0c<code>a<\/code>\u662f\u53d8\u91cf\uff0c\u800c<code>'abc'<\/code>\u624d\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\uff01\u6709\u4e9b\u65f6\u5019\uff0c\u6211\u4eec\u7ecf\u5e38\u8bf4\uff0c\u5bf9\u8c61<code>a<\/code>\u7684\u5185\u5bb9\u662f<code>'abc'<\/code>\uff0c\u4f46\u5176\u5b9e\u662f\u6307\uff0c<code>a<\/code>\u672c\u8eab\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u5b83\u6307\u5411\u7684\u5bf9\u8c61\u7684\u5185\u5bb9\u624d\u662f<code>'abc'<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u250c\u2500\u2500\u2500\u2510                  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 a \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500&gt;\u2502 'abc' \u2502\n\u2514\u2500\u2500\u2500\u2518                  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u8c03\u7528<code>a.replace('a', 'A')<\/code>\u65f6\uff0c\u5b9e\u9645\u4e0a\u8c03\u7528\u65b9\u6cd5<code>replace<\/code>\u662f\u4f5c\u7528\u5728\u5b57\u7b26\u4e32\u5bf9\u8c61<code>'abc'<\/code>\u4e0a\u7684\uff0c\u800c\u8fd9\u4e2a\u65b9\u6cd5\u867d\u7136\u540d\u5b57\u53eb<code>replace<\/code>\uff0c\u4f46\u5374\u6ca1\u6709\u6539\u53d8\u5b57\u7b26\u4e32<code>'abc'<\/code>\u7684\u5185\u5bb9\u3002\u76f8\u53cd\uff0c<code>replace<\/code>\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32<code>'Abc'<\/code>\u5e76\u8fd4\u56de\uff0c\u5982\u679c\u6211\u4eec\u7528\u53d8\u91cf<code>b<\/code>\u6307\u5411\u8be5\u65b0\u5b57\u7b26\u4e32\uff0c\u5c31\u5bb9\u6613\u7406\u89e3\u4e86\uff0c\u53d8\u91cf<code>a<\/code>\u4ecd\u6307\u5411\u539f\u6709\u7684\u5b57\u7b26\u4e32<code>'abc'<\/code>\uff0c\u4f46\u53d8\u91cf<code>b<\/code>\u5374\u6307\u5411\u65b0\u5b57\u7b26\u4e32<code>'Abc'<\/code>\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u250c\u2500\u2500\u2500\u2510                  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 a \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500&gt;\u2502 'abc' \u2502\n\u2514\u2500\u2500\u2500\u2518                  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n\u250c\u2500\u2500\u2500\u2510                  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 b \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500&gt;\u2502 'Abc' \u2502\n\u2514\u2500\u2500\u2500\u2518                  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5bf9\u4e8e\u4e0d\u53d8\u5bf9\u8c61\u6765\u8bf4\uff0c\u8c03\u7528\u5bf9\u8c61\u81ea\u8eab\u7684\u4efb\u610f\u65b9\u6cd5\uff0c\u4e5f\u4e0d\u4f1a\u6539\u53d8\u8be5\u5bf9\u8c61\u81ea\u8eab\u7684\u5185\u5bb9\u3002\u76f8\u53cd\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4f1a\u521b\u5efa\u65b0\u7684\u5bf9\u8c61\u5e76\u8fd4\u56de\uff0c\u8fd9\u6837\uff0c\u5c31\u4fdd\u8bc1\u4e86\u4e0d\u53ef\u53d8\u5bf9\u8c61\u672c\u8eab\u6c38\u8fdc\u662f\u4e0d\u53ef\u53d8\u7684\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\">\u4e8c\u3001<\/p>\n\n\n\n<p>\u5217\u8868\uff1alist\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u968f\u65f6\u6dfb\u52a0\u548c\u5220\u9664\u5176\u4e2d\u7684\u5143\u7d20,\u7528\u4e2d\u62ec\u53f7[]\u62ec\u8d77\u6765<\/p>\n\n\n\n<p>\u5143\u7ec4\uff1atuple\u548clist\u975e\u5e38\u7c7b\u4f3c\uff0c\u4f46\u662ftuple\u4e00\u65e6\u521d\u59cb\u5316\u5c31\u4e0d\u80fd\u4fee\u6539,\u7528\u5c0f\u62ec\u53f7()\u62ec\u8d77\u6765\uff0c\u4e5f\u6ca1\u6709append()\uff0cinsert()\u8fd9\u6837\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u8981\u5b9a\u4e49\u4e00\u4e2a\u53ea\u67091\u4e2a\u5143\u7d20\u7684tuple\uff0c\u5982\u679c\u4f60\u8fd9\u4e48\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; t = (1)\n&gt;&gt;&gt; t\n1\n<\/code><\/pre>\n\n\n\n<p>\u5b9a\u4e49\u7684\u4e0d\u662ftuple\uff0c\u662f<code>1<\/code>\u8fd9\u4e2a\u6570\uff01\u8fd9\u662f\u56e0\u4e3a\u62ec\u53f7<code>()<\/code>\u65e2\u53ef\u4ee5\u8868\u793atuple\uff0c\u53c8\u53ef\u4ee5\u8868\u793a\u6570\u5b66\u516c\u5f0f\u4e2d\u7684\u5c0f\u62ec\u53f7\uff0c\u8fd9\u5c31\u4ea7\u751f\u4e86\u6b67\u4e49\uff0c\u56e0\u6b64\uff0cPython\u89c4\u5b9a\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6309\u5c0f\u62ec\u53f7\u8fdb\u884c\u8ba1\u7b97\uff0c\u8ba1\u7b97\u7ed3\u679c\u81ea\u7136\u662f<code>1<\/code>\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u53ea\u67091\u4e2a\u5143\u7d20\u7684tuple\u5b9a\u4e49\u65f6\u5fc5\u987b\u52a0\u4e00\u4e2a\u9017\u53f7<code>,<\/code>\uff0c\u6765\u6d88\u9664\u6b67\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; t = (1,)\n&gt;&gt;&gt; t\n(1,)\n<\/code><\/pre>\n\n\n\n<p>Python\u5728\u663e\u793a\u53ea\u67091\u4e2a\u5143\u7d20\u7684tuple\u65f6\uff0c\u4e5f\u4f1a\u52a0\u4e00\u4e2a\u9017\u53f7<code>,<\/code>\uff0c\u4ee5\u514d\u4f60\u8bef\u89e3\u6210\u6570\u5b66\u8ba1\u7b97\u610f\u4e49\u4e0a\u7684\u62ec\u53f7\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u6765\u770b\u4e00\u4e2a\u201c\u53ef\u53d8\u7684\u201dtuple\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; t = ('a', 'b', &#91;'A', 'B'])\n&gt;&gt;&gt; t&#91;2]&#91;0] = 'X'\n&gt;&gt;&gt; t&#91;2]&#91;1] = 'Y'\n&gt;&gt;&gt; t\n('a', 'b', &#91;'X', 'Y'])\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2atuple\u5b9a\u4e49\u7684\u65f6\u5019\u67093\u4e2a\u5143\u7d20\uff0c\u5206\u522b\u662f<code>'a'<\/code>\uff0c<code>'b'<\/code>\u548c\u4e00\u4e2alist\uff0ctuple\u6240\u8c13\u7684\u201c\u4e0d\u53d8\u201d\u662f\u8bf4\uff0ctuple\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u6307\u5411\u6c38\u8fdc\u4e0d\u53d8\u3002\u5373\u6307\u5411<code>'a'<\/code>\uff0c\u5c31\u4e0d\u80fd\u6539\u6210\u6307\u5411<code>'b'<\/code>\uff0c\u6307\u5411\u4e00\u4e2alist\uff0c\u5c31\u4e0d\u80fd\u6539\u6210\u6307\u5411\u5176\u4ed6\u5bf9\u8c61\uff0c\u4f46\u6307\u5411\u7684\u8fd9\u4e2alist\u672c\u8eab\u662f\u53ef\u53d8\u7684\uff01<\/p>\n\n\n\n<p>\u8fd9\u4e24\u4e2a\u90fd\u662f\u53ef\u8fed\u4ee3\u7684\u3002<\/p>\n\n\n\n<p>for&#8230;in\u5faa\u73af\uff0c\u4f9d\u6b21\u628alist\u6216tuple\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fed\u4ee3\u51fa\u6765\uff0c\u770b\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>names = &#91;'Michael', 'Bob', 'Tracy']\n<strong>for<\/strong> name <strong>in<\/strong> names:\n    print(name)\n<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4f1a\u4f9d\u6b21\u6253\u5370<code>names<\/code>\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Michael\nBob\nTracy\n<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5<code>for x in ...<\/code>\u5faa\u73af\u5c31\u662f<strong>\u628a\u6bcf\u4e2a\u5143\u7d20\u4ee3\u5165\u53d8\u91cf<code>x<\/code><\/strong>\uff0c\u7136\u540e\u6267\u884c\u7f29\u8fdb\u5757\u7684\u8bed\u53e5\u3002<\/p>\n\n\n\n<p>\u518d\u6bd4\u5982\u6211\u4eec\u60f3\u8ba1\u7b971-10\u7684\u6574\u6570\u4e4b\u548c\uff0c\u53ef\u4ee5\u7528\u4e00\u4e2a<code>sum<\/code>\u53d8\u91cf\u505a\u7d2f\u52a0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sum = 0\n<strong>for<\/strong> x <strong>in<\/strong> &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:\n    sum = sum + x\nprint(sum)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Python\u7684<code>for<\/code>\u5faa\u73af\u4e0d\u4ec5\u53ef\u4ee5\u7528\u5728<code>list<\/code>\u6216<code>tuple<\/code>\u4e0a\uff0c\u8fd8\u53ef\u4ee5\u4f5c\u7528\u5728\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e0a\uff0c\u6bd4\u5982dict<\/p>\n\n\n\n<p>\u4f46\u56e0\u4e3a<code>dict<\/code>\u7684\u5b58\u50a8\u4e0d\u662f\u6309\u7167<code>list<\/code>\u7684\u65b9\u5f0f\u987a\u5e8f\u6392\u5217\uff0c\u6240\u4ee5\uff0c\u8fed\u4ee3\u51fa\u7684\u7ed3\u679c<strong>\u987a\u5e8f\u5f88\u53ef\u80fd\u4e0d\u4e00\u6837<\/strong>\u3002<\/p>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>dict<\/code>\u8fed\u4ee3\u7684\u662fkey\u3002\u5982\u679c\u8981\u8fed\u4ee3value\uff0c\u53ef\u4ee5\u7528<code>for value in d.values()<\/code>\uff0c\u5982\u679c\u8981\u540c\u65f6\u8fed\u4ee3key\u548cvalue\uff0c\u53ef\u4ee5\u7528<code>for k, v in d.items()<\/code>\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f5c\u7528\u4e8e<code>for<\/code>\u5faa\u73af\u7684\u6570\u636e\u7c7b\u578b\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n\n\n\n<p>\u4e00\u7c7b\u662f\u96c6\u5408\u6570\u636e\u7c7b\u578b\uff0c\u5982<code>list<\/code>\u3001<code>tuple<\/code>\u3001<code>dict<\/code>\u3001<code>set<\/code>\u3001<code>str<\/code>\u7b49\uff1b<\/p>\n\n\n\n<p>\u4e00\u7c7b\u662f<code>generator<\/code>\uff0c\u5305\u62ec\u751f\u6210\u5668\u548c\u5e26<code>yield<\/code>\u7684generator function\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u53ef\u4ee5\u76f4\u63a5\u4f5c\u7528\u4e8e<code>for<\/code>\u5faa\u73af\u7684\u5bf9\u8c61\u7edf\u79f0\u4e3a<strong>\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/strong>\uff1a<code>Iterable<\/code>\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f<code>Iterable<\/code>\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; from collections.abc import Iterable\n&gt;&gt;&gt; isinstance(&#91;], Iterable)\nTrue\n&gt;&gt;&gt; isinstance({}, Iterable)\nTrue\n&gt;&gt;&gt; isinstance('abc', Iterable)\nTrue\n&gt;&gt;&gt; isinstance((x for x in range(10)), Iterable)\nTrue\n&gt;&gt;&gt; isinstance(100, Iterable)\nFalse\n<\/code><\/pre>\n\n\n\n<p>\u800c\u751f\u6210\u5668\u4e0d\u4f46\u53ef\u4ee5\u4f5c\u7528\u4e8e<code>for<\/code>\u5faa\u73af\uff0c\u8fd8\u53ef\u4ee5\u88ab<code>next()<\/code>\u51fd\u6570\u4e0d\u65ad\u8c03\u7528\u5e76\u8fd4\u56de\u4e0b\u4e00\u4e2a\u503c\uff0c\u76f4\u5230\u6700\u540e\u629b\u51fa<code>StopIteration<\/code>\u9519\u8bef\u8868\u793a\u65e0\u6cd5\u7ee7\u7eed\u8fd4\u56de\u4e0b\u4e00\u4e2a\u503c\u4e86\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u88ab<code>next()<\/code>\u51fd\u6570\u8c03\u7528\u5e76\u4e0d\u65ad\u8fd4\u56de\u4e0b\u4e00\u4e2a\u503c\u7684\u5bf9\u8c61\u79f0\u4e3a<strong>\u8fed\u4ee3\u5668<\/strong>\uff1a<code>Iterator<\/code>\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f<code>Iterator<\/code>\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; from collections.abc import Iterator\n&gt;&gt;&gt; isinstance((x for x in range(10)), Iterator)\nTrue\n&gt;&gt;&gt; isinstance(&#91;], Iterator)\nFalse\n&gt;&gt;&gt; isinstance({}, Iterator)\nFalse\n&gt;&gt;&gt; isinstance('abc', Iterator)\nFalse\n<\/code><\/pre>\n\n\n\n<p>\u751f\u6210\u5668\u90fd\u662f<code>Iterator<\/code>\u5bf9\u8c61\uff0c\u4f46<code>list<\/code>\u3001<code>dict<\/code>\u3001<code>str<\/code>\u867d\u7136\u662f<code>Iterable<\/code>\uff0c\u5374\u4e0d\u662f<code>Iterator<\/code>\u3002<\/p>\n\n\n\n<p>\u628a<code>list<\/code>\u3001<code>dict<\/code>\u3001<code>str<\/code>\u7b49<code>Iterable<\/code>\u53d8\u6210<code>Iterator<\/code>\u53ef\u4ee5\u4f7f\u7528<code>iter()<\/code>\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; isinstance(iter(&#91;]), Iterator)\nTrue\n&gt;&gt;&gt; isinstance(iter('abc'), Iterator)\nTrue\n<\/code><\/pre>\n\n\n\n<p>\u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u4e3a\u4ec0\u4e48<code>list<\/code>\u3001<code>dict<\/code>\u3001<code>str<\/code>\u7b49\u6570\u636e\u7c7b\u578b\u4e0d\u662f<code>Iterator<\/code>\uff1f<\/p>\n\n\n\n<p>\u8fd9\u662f\u56e0\u4e3aPython\u7684<code>Iterator<\/code>\u5bf9\u8c61\u8868\u793a\u7684\u662f\u4e00\u4e2a\u6570\u636e\u6d41\uff0c<code>Iterator<\/code>\u5bf9\u8c61\u53ef\u4ee5\u88ab<code>next()<\/code>\u51fd\u6570\u8c03\u7528\u5e76\u4e0d\u65ad\u8fd4\u56de\u4e0b\u4e00\u4e2a\u6570\u636e\uff0c\u76f4\u5230\u6ca1\u6709\u6570\u636e\u65f6\u629b\u51fa<code>StopIteration<\/code>\u9519\u8bef\u3002\u53ef\u4ee5\u628a\u8fd9\u4e2a\u6570\u636e\u6d41\u770b\u505a\u662f\u4e00\u4e2a\u6709\u5e8f\u5e8f\u5217\uff0c\u4f46\u6211\u4eec\u5374\u4e0d\u80fd\u63d0\u524d\u77e5\u9053\u5e8f\u5217\u7684\u957f\u5ea6\uff0c\u53ea\u80fd\u4e0d\u65ad\u901a\u8fc7<code>next()<\/code>\u51fd\u6570\u5b9e\u73b0\u6309\u9700\u8ba1\u7b97\u4e0b\u4e00\u4e2a\u6570\u636e\uff0c\u6240\u4ee5<code>Iterator<\/code>\u7684\u8ba1\u7b97\u662f\u60f0\u6027\u7684\uff0c\u53ea\u6709\u5728\u9700\u8981\u8fd4\u56de\u4e0b\u4e00\u4e2a\u6570\u636e\u65f6\u5b83\u624d\u4f1a\u8ba1\u7b97<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\">\u4e09\u3001<\/p>\n\n\n\n<p><code>if<\/code>\u8bed\u53e5\u6267\u884c\u6709\u4e2a\u7279\u70b9\uff0c\u5b83\u662f\u4ece\u4e0a\u5f80\u4e0b\u5224\u65ad\uff0c\u5982\u679c\u5728\u67d0\u4e2a\u5224\u65ad\u4e0a\u662f<code>True<\/code>\uff0c\u628a\u8be5\u5224\u65ad\u5bf9\u5e94\u7684\u8bed\u53e5\u6267\u884c\u540e\uff0c\u5c31<strong>\u5ffd\u7565<\/strong>\u6389\u5269\u4e0b\u7684<code>elif<\/code>\u548c<code>else<\/code><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53e6\u5916\u5f53\u6211\u4eec\u7528<code>if ... elif ... elif ... else ...<\/code>\u5224\u65ad\u65f6\uff0c\u4f1a\u5199\u5f88\u957f\u4e00\u4e32\u4ee3\u7801\uff0c\u53ef\u8bfb\u6027\u8f83\u5dee\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8981\u9488\u5bf9\u67d0\u4e2a\u53d8\u91cf\u5339\u914d\u82e5\u5e72\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>match<\/code>\u8bed\u53e5\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u67d0\u4e2a\u5b66\u751f\u7684\u6210\u7ee9\u53ea\u80fd\u662f<code>A<\/code>\u3001<code>B<\/code>\u3001<code>C<\/code>\uff0c\u7528<code>if<\/code>\u8bed\u53e5\u7f16\u5199\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>score = 'B'\nif score == 'A':\n    print('score is A.')\nelif score == 'B':\n    print('score is B.')\nelif score == 'C':\n    print('score is C.')\nelse:\n    print('invalid score.')\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u7528<code>match<\/code>\u8bed\u53e5\u6539\u5199\uff0c\u5219\u6539\u5199\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>score = 'B'\n\nmatch score:\n    case 'A':\n        print('score is A.')\n    case 'B':\n        print('score is B.')\n    case 'C':\n        print('score is C.')\n    case _: # _\u8868\u793a\u5339\u914d\u5230\u5176\u4ed6\u4efb\u4f55\u60c5\u51b5\n        print('score is ???.')\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528<code>match<\/code>\u8bed\u53e5\u65f6\uff0c\u6211\u4eec\u4f9d\u6b21\u7528<code>case xxx<\/code>\u5339\u914d\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u6700\u540e\uff08\u4e14\u4ec5\u80fd\u5728\u6700\u540e\uff09\u52a0\u4e00\u4e2a<code>case _<\/code>\u8868\u793a\u201c\u4efb\u610f\u503c\u201d<\/p>\n\n\n\n<p><code>match<\/code>\u8bed\u53e5\u8fd8\u53ef\u4ee5\u5339\u914d\u5217\u8868\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5047\u8bbe\u7528\u6237\u8f93\u5165\u4e86\u4e00\u4e2a\u547d\u4ee4\uff0c\u7528<code>args = ['gcc', 'hello.c']<\/code>\u5b58\u50a8\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u7528<code>match<\/code>\u5339\u914d\u6765\u89e3\u6790\u8fd9\u4e2a\u5217\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>args = &#91;'gcc', 'hello.c', 'world.c']\n# args = &#91;'clean']\n# args = &#91;'gcc']\n\nmatch args:\n    # \u5982\u679c\u4ec5\u51fa\u73b0gcc\uff0c\u62a5\u9519:\n    case &#91;'gcc']:\n        print('gcc: missing source file(s).')\n    # \u51fa\u73b0gcc\uff0c\u4e14\u81f3\u5c11\u6307\u5b9a\u4e86\u4e00\u4e2a\u6587\u4ef6:\n    case &#91;'gcc', file1, *files]:\n        print('gcc compile: ' + file1 + ', ' + ', '.join(files))\n    # \u4ec5\u51fa\u73b0clean:\n    case &#91;'clean']:\n        print('clean')\n    case _:\n        print('invalid command.')\n<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e00\u4e2a<code>case ['gcc']<\/code>\u8868\u793a\u5217\u8868\u4ec5\u6709<code>'gcc'<\/code>\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6ca1\u6709\u6307\u5b9a\u6587\u4ef6\u540d\uff0c\u62a5\u9519\uff1b<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u4e2a<code>case ['gcc', file1, *files]<\/code>\u8868\u793a\u5217\u8868\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f<code>'gcc'<\/code>\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u7ed1\u5b9a\u5230\u53d8\u91cf<code>file1<\/code>\uff0c\u540e\u9762\u7684\u4efb\u610f\u4e2a\u5b57\u7b26\u4e32\u7ed1\u5b9a\u5230<code>*files<\/code>\uff08\u7b26\u53f7<code>*<\/code>\u7684\u4f5c\u7528\u5c06\u5728<a href=\"https:\/\/liaoxuefeng.com\/books\/python\/function\/parameter\/index.html\">\u51fd\u6570\u7684\u53c2\u6570<\/a>\u4e2d\u8bb2\u89e3\uff09\uff0c\u5b83\u5b9e\u9645\u4e0a\u8868\u793a\u81f3\u5c11\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\uff1b<\/p>\n\n\n\n<p>\u7b2c\u4e09\u4e2a<code>case ['clean']<\/code>\u8868\u793a\u5217\u8868\u4ec5\u6709<code>'clean'<\/code>\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1b<\/p>\n\n\n\n<p>\u6700\u540e\u4e00\u4e2a<code>case _<\/code>\u8868\u793a\u5176\u4ed6\u6240\u6709\u60c5\u51b5\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\">\u56db\u3001<\/p>\n\n\n\n<p>\u6458\u81ea<a href=\"https:\/\/liaoxuefeng.com\/books\/python\/function\/parameter\/index.html\">https:\/\/liaoxuefeng.com\/books\/python\/function\/parameter\/index.html<\/a><\/p>\n\n\n\n<p>\u9ed8\u8ba4\u53c2\u6570\u6709\u4e2a\u6700\u5927\u7684\u5751\uff0c\u6f14\u793a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u5148\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u4f20\u5165\u4e00\u4e2alist\uff0c\u6dfb\u52a0\u4e00\u4e2a<code>END<\/code>\u518d\u8fd4\u56de\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_end(L=&#91;]):\n    L.append('END')\n    return L\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u4f60\u6b63\u5e38\u8c03\u7528\u65f6\uff0c\u7ed3\u679c\u4f3c\u4e4e\u4e0d\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; add_end(&#91;1, 2, 3])\n&#91;1, 2, 3, 'END']\n&gt;&gt;&gt; add_end(&#91;'x', 'y', 'z'])\n&#91;'x', 'y', 'z', 'END']\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u4f60\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\u8c03\u7528\u65f6\uff0c\u4e00\u5f00\u59cb\u7ed3\u679c\u4e5f\u662f\u5bf9\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; add_end()\n&#91;'END']\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\uff0c\u518d\u6b21\u8c03\u7528<code>add_end()<\/code>\u65f6\uff0c\u7ed3\u679c\u5c31\u4e0d\u5bf9\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; add_end()\n&#91;'END', 'END']\n&gt;&gt;&gt; add_end()\n&#91;'END', 'END', 'END']\n<\/code><\/pre>\n\n\n\n<p>\u5f88\u591a\u521d\u5b66\u8005\u5f88\u7591\u60d1\uff0c\u9ed8\u8ba4\u53c2\u6570\u662f<code>[]<\/code>\uff0c\u4f46\u662f\u51fd\u6570\u4f3c\u4e4e\u6bcf\u6b21\u90fd\u201c\u8bb0\u4f4f\u4e86\u201d\u4e0a\u6b21\u6dfb\u52a0\u4e86<code>'END'<\/code>\u540e\u7684list\u3002<\/p>\n\n\n\n<p>\u539f\u56e0\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>Python\u51fd\u6570\u5728\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u53c2\u6570<code>L<\/code>\u7684\u503c\u5c31\u88ab\u8ba1\u7b97\u51fa\u6765\u4e86\uff0c\u5373<code>[]<\/code>\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u53c2\u6570<code>L<\/code>\u4e5f\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u5b83\u6307\u5411\u5bf9\u8c61<code>[]<\/code>\uff0c\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5982\u679c\u6539\u53d8\u4e86<code>L<\/code>\u7684\u5185\u5bb9\uff0c\u5219\u4e0b\u6b21\u8c03\u7528\u65f6\uff0c\u9ed8\u8ba4\u53c2\u6570\u7684\u5185\u5bb9\u5c31\u53d8\u4e86\uff0c\u4e0d\u518d\u662f\u51fd\u6570\u5b9a\u4e49\u65f6\u7684<code>[]<\/code>\u4e86\u3002<\/p>\n\n\n\n<p>&nbsp;\u7279\u522b\u6ce8\u610f<\/p>\n\n\n\n<p>\u5b9a\u4e49\u9ed8\u8ba4\u53c2\u6570\u8981\u7262\u8bb0\u4e00\u70b9\uff1a\u9ed8\u8ba4\u53c2\u6570\u5fc5\u987b\u6307\u5411\u4e0d\u53d8\u5bf9\u8c61\uff01<\/p>\n\n\n\n<p>\u8981\u4fee\u6539\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u7528<code>None<\/code>\u8fd9\u4e2a\u4e0d\u53d8\u5bf9\u8c61\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_end(L=None):\n    if L is None:\n        L = &#91;]\n    L.append('END')\n    return L\n<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u65e0\u8bba\u8c03\u7528\u591a\u5c11\u6b21\uff0c\u90fd\u4e0d\u4f1a\u6709\u95ee\u9898\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; add_end()\n&#91;'END']\n&gt;&gt;&gt; add_end()\n&#91;'END']\n<\/code><\/pre>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u8981\u8bbe\u8ba1<code>str<\/code>\u3001<code>None<\/code>\u8fd9\u6837\u7684\u4e0d\u53d8\u5bf9\u8c61\u5462\uff1f\u56e0\u4e3a\u4e0d\u53d8\u5bf9\u8c61\u4e00\u65e6\u521b\u5efa\uff0c\u5bf9\u8c61\u5185\u90e8\u7684\u6570\u636e\u5c31\u4e0d\u80fd\u4fee\u6539\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u7531\u4e8e\u4fee\u6539\u6570\u636e\u5bfc\u81f4\u7684\u9519\u8bef\u3002\u6b64\u5916\uff0c\u7531\u4e8e\u5bf9\u8c61\u4e0d\u53d8\uff0c\u591a\u4efb\u52a1\u73af\u5883\u4e0b\u540c\u65f6\u8bfb\u53d6\u5bf9\u8c61\u4e0d\u9700\u8981\u52a0\u9501\uff0c\u540c\u65f6\u8bfb\u4e00\u70b9\u95ee\u9898\u90fd\u6ca1\u6709\u3002\u6211\u4eec\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u5982\u679c\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a\u4e0d\u53d8\u5bf9\u8c61\uff0c\u90a3\u5c31\u5c3d\u91cf\u8bbe\u8ba1\u6210\u4e0d\u53d8\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53ef\u53d8\u53c2\u6570<\/h3>\n\n\n\n<p>\u5728Python\u51fd\u6570\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u53ef\u53d8\u53c2\u6570\u3002\u987e\u540d\u601d\u4e49\uff0c\u53ef\u53d8\u53c2\u6570\u5c31\u662f\u4f20\u5165\u7684\u53c2\u6570\u4e2a\u6570\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u662f1\u4e2a\u30012\u4e2a\u5230\u4efb\u610f\u4e2a\uff0c\u8fd8\u53ef\u4ee5\u662f0\u4e2a\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4ee5\u6570\u5b66\u9898\u4e3a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u4e00\u7ec4\u6570\u5b57a\uff0cb\uff0cc\u2026\u2026\uff0c\u8bf7\u8ba1\u7b97a<sup>2<\/sup>&nbsp;+ b<sup>2<\/sup>&nbsp;+ c<sup>2<\/sup>&nbsp;+ \u2026\u2026\u3002<\/p>\n\n\n\n<p>\u8981\u5b9a\u4e49\u51fa\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u5b9a\u8f93\u5165\u7684\u53c2\u6570\u3002\u7531\u4e8e\u53c2\u6570\u4e2a\u6570\u4e0d\u786e\u5b9a\uff0c\u6211\u4eec\u9996\u5148\u60f3\u5230\u53ef\u4ee5\u628aa\uff0cb\uff0cc\u2026\u2026\u4f5c\u4e3a\u4e00\u4e2alist\u6216tuple\u4f20\u8fdb\u6765\uff0c\u8fd9\u6837\uff0c\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calc(numbers):\n    sum = 0\n    for n in numbers:\n        sum = sum + n * n\n    return sum\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u8c03\u7528\u7684\u65f6\u5019\uff0c\u9700\u8981\u5148\u7ec4\u88c5\u51fa\u4e00\u4e2alist\u6216tuple\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; calc(&#91;1, 2, 3])\n14\n&gt;&gt;&gt; calc((1, 3, 5, 7))\n84\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5229\u7528\u53ef\u53d8\u53c2\u6570\uff0c\u8c03\u7528\u51fd\u6570\u7684\u65b9\u5f0f\u53ef\u4ee5\u7b80\u5316\u6210\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; calc(1, 2, 3)\n14\n&gt;&gt;&gt; calc(1, 3, 5, 7)\n84\n<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\uff0c\u6211\u4eec\u628a\u51fd\u6570\u7684\u53c2\u6570\u6539\u4e3a\u53ef\u53d8\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calc(*numbers):\n    sum = 0\n    for n in numbers:\n        sum = sum + n * n\n    return sum\n<\/code><\/pre>\n\n\n\n<p>\u5b9a\u4e49\u53ef\u53d8\u53c2\u6570\u548c\u5b9a\u4e49\u4e00\u4e2alist\u6216tuple\u53c2\u6570\u76f8\u6bd4\uff0c\u4ec5\u4ec5\u5728\u53c2\u6570\u524d\u9762\u52a0\u4e86\u4e00\u4e2a<code>*<\/code>\u53f7\u3002\u5728\u51fd\u6570\u5185\u90e8\uff0c\u53c2\u6570<code>numbers<\/code>\u63a5\u6536\u5230\u7684\u662f\u4e00\u4e2atuple\uff0c\u56e0\u6b64\uff0c\u51fd\u6570\u4ee3\u7801\u5b8c\u5168\u4e0d\u53d8\u3002\u4f46\u662f\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u4efb\u610f\u4e2a\u53c2\u6570\uff0c\u5305\u62ec0\u4e2a\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; calc(1, 2)\n5\n&gt;&gt;&gt; calc()\n0\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5df2\u7ecf\u6709\u4e00\u4e2alist\u6216\u8005tuple\uff0c\u8981\u8c03\u7528\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570\u600e\u4e48\u529e\uff1f\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; nums = &#91;1, 2, 3]\n&gt;&gt;&gt; calc(nums&#91;0], nums&#91;1], nums&#91;2])\n14\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u5199\u6cd5\u5f53\u7136\u662f\u53ef\u884c\u7684\uff0c\u95ee\u9898\u662f\u592a\u7e41\u7410\uff0c\u6240\u4ee5Python\u5141\u8bb8\u4f60\u5728list\u6216tuple\u524d\u9762\u52a0\u4e00\u4e2a<code>*<\/code>\u53f7\uff0c\u628alist\u6216tuple\u7684\u5143\u7d20\u53d8\u6210\u53ef\u53d8\u53c2\u6570\u4f20\u8fdb\u53bb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; nums = &#91;1, 2, 3]\n&gt;&gt;&gt; calc(*nums)\n14\n<\/code><\/pre>\n\n\n\n<p><code>*nums<\/code>\u8868\u793a\u628a<code>nums<\/code>\u8fd9\u4e2alist\u7684\u6240\u6709\u5143\u7d20\u4f5c\u4e3a\u53ef\u53d8\u53c2\u6570\u4f20\u8fdb\u53bb\u3002\u8fd9\u79cd\u5199\u6cd5\u76f8\u5f53\u6709\u7528\uff0c\u800c\u4e14\u5f88\u5e38\u89c1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u952e\u5b57\u53c2\u6570<\/h3>\n\n\n\n<p>\u53ef\u53d8\u53c2\u6570\u5141\u8bb8\u4f60\u4f20\u51650\u4e2a\u6216\u4efb\u610f\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e9b\u53ef\u53d8\u53c2\u6570\u5728\u51fd\u6570\u8c03\u7528\u65f6\u81ea\u52a8\u7ec4\u88c5\u4e3a\u4e00\u4e2atuple\u3002\u800c\u5173\u952e\u5b57\u53c2\u6570\u5141\u8bb8\u4f60\u4f20\u51650\u4e2a\u6216\u4efb\u610f\u4e2a\u542b\u53c2\u6570\u540d\u7684\u53c2\u6570\uff0c\u8fd9\u4e9b\u5173\u952e\u5b57\u53c2\u6570\u5728\u51fd\u6570\u5185\u90e8\u81ea\u52a8\u7ec4\u88c5\u4e3a\u4e00\u4e2adict\u3002\u8bf7\u770b\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def person(name, age, **kw):\n    print('name:', name, 'age:', age, 'other:', kw)\n<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570<code>person<\/code>\u9664\u4e86\u5fc5\u9009\u53c2\u6570<code>name<\/code>\u548c<code>age<\/code>\u5916\uff0c\u8fd8\u63a5\u53d7\u5173\u952e\u5b57\u53c2\u6570<code>kw<\/code>\u3002\u5728\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u53ea\u4f20\u5165\u5fc5\u9009\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; person('Michael', 30)\nname: Michael age: 30 other: {}\n<\/code><\/pre>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u4f20\u5165\u4efb\u610f\u4e2a\u6570\u7684\u5173\u952e\u5b57\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; person('Bob', 35, city='Beijing')\nname: Bob age: 35 other: {'city': 'Beijing'}\n&gt;&gt;&gt; person('Adam', 45, gender='M', job='Engineer')\nname: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}\n<\/code><\/pre>\n\n\n\n<p>\u5173\u952e\u5b57\u53c2\u6570\u6709\u4ec0\u4e48\u7528\uff1f\u5b83\u53ef\u4ee5\u6269\u5c55\u51fd\u6570\u7684\u529f\u80fd\u3002\u6bd4\u5982\uff0c\u5728<code>person<\/code>\u51fd\u6570\u91cc\uff0c\u6211\u4eec\u4fdd\u8bc1\u80fd\u63a5\u6536\u5230<code>name<\/code>\u548c<code>age<\/code>\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u4f46\u662f\uff0c\u5982\u679c\u8c03\u7528\u8005\u613f\u610f\u63d0\u4f9b\u66f4\u591a\u7684\u53c2\u6570\uff0c\u6211\u4eec\u4e5f\u80fd\u6536\u5230\u3002\u8bd5\u60f3\u4f60\u6b63\u5728\u505a\u4e00\u4e2a\u7528\u6237\u6ce8\u518c\u7684\u529f\u80fd\uff0c\u9664\u4e86\u7528\u6237\u540d\u548c\u5e74\u9f84\u662f\u5fc5\u586b\u9879\u5916\uff0c\u5176\u4ed6\u90fd\u662f\u53ef\u9009\u9879\uff0c\u5229\u7528\u5173\u952e\u5b57\u53c2\u6570\u6765\u5b9a\u4e49\u8fd9\u4e2a\u51fd\u6570\u5c31\u80fd\u6ee1\u8db3\u6ce8\u518c\u7684\u9700\u6c42\u3002<\/p>\n\n\n\n<p>\u548c\u53ef\u53d8\u53c2\u6570\u7c7b\u4f3c\uff0c\u4e5f\u53ef\u4ee5\u5148\u7ec4\u88c5\u51fa\u4e00\u4e2adict\uff0c\u7136\u540e\uff0c\u628a\u8be5dict\u8f6c\u6362\u4e3a\u5173\u952e\u5b57\u53c2\u6570\u4f20\u8fdb\u53bb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; extra = {'city': 'Beijing', 'job': 'Engineer'}\n&gt;&gt;&gt; person('Jack', 24, city=extra&#91;'city'], job=extra&#91;'job'])\nname: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u7136\uff0c\u4e0a\u9762\u590d\u6742\u7684\u8c03\u7528\u53ef\u4ee5\u7528\u7b80\u5316\u7684\u5199\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; extra = {'city': 'Beijing', 'job': 'Engineer'}\n&gt;&gt;&gt; person('Jack', 24, **extra)\nname: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}\n<\/code><\/pre>\n\n\n\n<p><code>**extra<\/code>\u8868\u793a\u628a<code>extra<\/code>\u8fd9\u4e2adict\u7684\u6240\u6709key-value\u7528\u5173\u952e\u5b57\u53c2\u6570\u4f20\u5165\u5230\u51fd\u6570\u7684<code>**kw<\/code>\u53c2\u6570\uff0c<code>kw<\/code>\u5c06\u83b7\u5f97\u4e00\u4e2adict\uff0c\u6ce8\u610f<code>kw<\/code>\u83b7\u5f97\u7684dict\u662f<code>extra<\/code>\u7684\u4e00\u4efd\u62f7\u8d1d\uff0c\u5bf9<code>kw<\/code>\u7684\u6539\u52a8\u4e0d\u4f1a\u5f71\u54cd\u5230\u51fd\u6570\u5916\u7684<code>extra<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u5173\u952e\u5b57\u53c2\u6570\uff0c\u51fd\u6570\u7684\u8c03\u7528\u8005\u53ef\u4ee5\u4f20\u5165\u4efb\u610f\u4e0d\u53d7\u9650\u5236\u7684\u5173\u952e\u5b57\u53c2\u6570\u3002\u81f3\u4e8e\u5230\u5e95\u4f20\u5165\u4e86\u54ea\u4e9b\uff0c\u5c31\u9700\u8981\u5728\u51fd\u6570\u5185\u90e8\u901a\u8fc7<code>kw<\/code>\u68c0\u67e5\u3002<\/p>\n\n\n\n<p>\u4ecd\u4ee5<code>person()<\/code>\u51fd\u6570\u4e3a\u4f8b\uff0c\u6211\u4eec\u5e0c\u671b\u68c0\u67e5\u662f\u5426\u6709<code>city<\/code>\u548c<code>job<\/code>\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def person(name, age, **kw):\n    if 'city' in kw:\n        # \u6709city\u53c2\u6570\n        pass\n    if 'job' in kw:\n        # \u6709job\u53c2\u6570\n        pass\n    print('name:', name, 'age:', age, 'other:', kw)\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u8c03\u7528\u8005\u4ecd\u53ef\u4ee5\u4f20\u5165\u4e0d\u53d7\u9650\u5236\u7684\u5173\u952e\u5b57\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; person('Jack', 24, city='Beijing', addr='Chaoyang', zipcode=123456)\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u8981\u9650\u5236\u5173\u952e\u5b57\u53c2\u6570\u7684\u540d\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\uff0c\u4f8b\u5982\uff0c\u53ea\u63a5\u6536<code>city<\/code>\u548c<code>job<\/code>\u4f5c\u4e3a\u5173\u952e\u5b57\u53c2\u6570\u3002\u8fd9\u79cd\u65b9\u5f0f\u5b9a\u4e49\u7684\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def person(name, age, *, city, job):\n    print(name, age, city, job)\n<\/code><\/pre>\n\n\n\n<p>\u548c\u5173\u952e\u5b57\u53c2\u6570<code>**kw<\/code>\u4e0d\u540c\uff0c\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\u9700\u8981\u4e00\u4e2a\u7279\u6b8a\u5206\u9694\u7b26<code>*<\/code>\uff0c<code>*<\/code>\u540e\u9762\u7684\u53c2\u6570\u88ab\u89c6\u4e3a\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u8c03\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; person('Jack', 24, city='Beijing', job='Engineer')\nJack 24 Beijing Engineer\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u51fd\u6570\u5b9a\u4e49\u4e2d\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570\uff0c\u540e\u9762\u8ddf\u7740\u7684\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\u5c31\u4e0d\u518d\u9700\u8981\u4e00\u4e2a\u7279\u6b8a\u5206\u9694\u7b26<code>*<\/code>\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def person(name, age, *args, city, job):\n    print(name, age, args, city, job)\n<\/code><\/pre>\n\n\n\n<p>\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\u5fc5\u987b\u4f20\u5165\u53c2\u6570\u540d\uff0c\u8fd9\u548c\u4f4d\u7f6e\u53c2\u6570\u4e0d\u540c\u3002\u5982\u679c\u6ca1\u6709\u4f20\u5165\u53c2\u6570\u540d\uff0c\u8c03\u7528\u5c06\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; person('Jack', 24, 'Beijing', 'Engineer')\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nTypeError: person() missing 2 required keyword-only arguments: 'city' and 'job'\n<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8e\u8c03\u7528\u65f6\u7f3a\u5c11\u53c2\u6570\u540d<code>city<\/code>\u548c<code>job<\/code>\uff0cPython\u89e3\u91ca\u5668\u628a\u524d\u4e24\u4e2a\u53c2\u6570\u89c6\u4e3a\u4f4d\u7f6e\u53c2\u6570\uff0c\u540e\u4e24\u4e2a\u53c2\u6570\u4f20\u7ed9<code>*args<\/code>\uff0c\u4f46\u7f3a\u5c11\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\u5bfc\u81f4\u62a5\u9519\u3002<\/p>\n\n\n\n<p>\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\u53ef\u4ee5\u6709\u7f3a\u7701\u503c\uff0c\u4ece\u800c\u7b80\u5316\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def person(name, age, *, city='Beijing', job):\n    print(name, age, city, job)\n<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8e\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570<code>city<\/code>\u5177\u6709\u9ed8\u8ba4\u503c\uff0c\u8c03\u7528\u65f6\uff0c\u53ef\u4e0d\u4f20\u5165<code>city<\/code>\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; person('Jack', 24, job='Engineer')\nJack 24 Beijing Engineer\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\u65f6\uff0c\u8981\u7279\u522b\u6ce8\u610f\uff0c\u5982\u679c\u6ca1\u6709\u53ef\u53d8\u53c2\u6570\uff0c\u5c31\u5fc5\u987b\u52a0\u4e00\u4e2a<code>*<\/code>\u4f5c\u4e3a\u7279\u6b8a\u5206\u9694\u7b26\u3002\u5982\u679c\u7f3a\u5c11<code>*<\/code>\uff0cPython\u89e3\u91ca\u5668\u5c06\u65e0\u6cd5\u8bc6\u522b\u4f4d\u7f6e\u53c2\u6570\u548c\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def person(name, age, city, job):\n    # \u7f3a\u5c11 *\uff0ccity\u548cjob\u88ab\u89c6\u4e3a\u4f4d\u7f6e\u53c2\u6570\n    pass\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u6570\u7ec4\u5408<\/h3>\n\n\n\n<p>\u5728Python\u4e2d\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u5fc5\u9009\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u548c\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\uff0c\u8fd95\u79cd\u53c2\u6570\u90fd\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\u3002\u4f46\u662f\u8bf7\u6ce8\u610f\uff0c\u53c2\u6570\u5b9a\u4e49\u7684\u987a\u5e8f\u5fc5\u987b\u662f\uff1a\u5fc5\u9009\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3001\u547d\u540d\u5173\u952e\u5b57\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u9ed8\u8ba4\u53c2\u6570\u4e00\u5b9a\u8981\u7528\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u5982\u679c\u662f\u53ef\u53d8\u5bf9\u8c61\uff0c\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4f1a\u6709\u903b\u8f91\u9519\u8bef\uff01<\/p>\n\n\n\n<p>\u8981\u6ce8\u610f\u5b9a\u4e49\u53ef\u53d8\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u7684\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<p><code>*args<\/code>\u662f\u53ef\u53d8\u53c2\u6570\uff0cargs\u63a5\u6536\u7684\u662f\u4e00\u4e2atuple\uff1b<\/p>\n\n\n\n<p><code>**kw<\/code>\u662f\u5173\u952e\u5b57\u53c2\u6570\uff0ckw\u63a5\u6536\u7684\u662f\u4e00\u4e2adict\u3002<\/p>\n\n\n\n<p>\u4ee5\u53ca\u8c03\u7528\u51fd\u6570\u65f6\u5982\u4f55\u4f20\u5165\u53ef\u53d8\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u7684\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<p>\u53ef\u53d8\u53c2\u6570\u65e2\u53ef\u4ee5\u76f4\u63a5\u4f20\u5165\uff1a<code>func(1, 2, 3)<\/code>\uff0c\u53c8\u53ef\u4ee5\u5148\u7ec4\u88c5list\u6216tuple\uff0c\u518d\u901a\u8fc7<code>*args<\/code>\u4f20\u5165\uff1a<code>func(*(1, 2, 3))<\/code>\uff1b<\/p>\n\n\n\n<p>\u5173\u952e\u5b57\u53c2\u6570\u65e2\u53ef\u4ee5\u76f4\u63a5\u4f20\u5165\uff1a<code>func(a=1, b=2)<\/code>\uff0c\u53c8\u53ef\u4ee5\u5148\u7ec4\u88c5dict\uff0c\u518d\u901a\u8fc7<code>**kw<\/code>\u4f20\u5165\uff1a<code>func(**{'a': 1, 'b': 2})<\/code><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\">\u4e94\uff1a<\/p>\n\n\n\n<p>Python\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u5c31\u53ef\u4ee5\u5bf9list\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; sorted(&#91;36, 5, -12, 9, -21])\n&#91;-21, -12, 5, 9, 36]\n<\/code><\/pre>\n\n\n\n<p>\u6b64\u5916\uff0c<code>sorted()<\/code>\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u8fd8\u53ef\u4ee5\u63a5\u6536\u4e00\u4e2a<code>key<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\uff0c\u4f8b\u5982\u6309\u7edd\u5bf9\u503c\u5927\u5c0f\u6392\u5e8f\uff1a<\/p>\n\n\n\n<p>&gt;&gt;&gt; sorted([36, 5, -12, 9, -21], key=abs)\n[5, 9, -12, -21, 36]\n<\/p>\n\n\n\n<p>key\u6307\u5b9a\u7684\u51fd\u6570\u5c06\u4f5c\u7528\u4e8elist\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u4e0a\uff0c\u5e76\u6839\u636ekey\u51fd\u6570\u8fd4\u56de\u7684\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f<\/p>\n\n\n\n<p>\u53ef\u5b9e\u73b0\u5ffd\u7565\u5927\u5c0f\u5199\u7684\u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; sorted(&#91;'bob', 'about', 'Zoo', 'Credit'], key=str.lower)\n&#91;'about', 'bob', 'Credit', 'Zoo']\n<\/code><\/pre>\n\n\n\n<p>\u8981\u8fdb\u884c\u53cd\u5411\u6392\u5e8f\uff0c\u4e0d\u5fc5\u6539\u52a8key\u51fd\u6570\uff0c\u53ef\u4ee5\u4f20\u5165\u7b2c\u4e09\u4e2a\u53c2\u6570<code>reverse=True<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; sorted(&#91;'bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=True)\n&#91;'Zoo', 'Credit', 'bob', 'about']\n<\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">\u516d\u3001<\/p>\n\n\n\n<p>Python\u5185\u5efa\u7684<code>filter()<\/code>\u51fd\u6570\u7528\u4e8e\u8fc7\u6ee4\u5e8f\u5217\u3002<\/p>\n\n\n\n<p>\u548c<code>map()<\/code>\u7c7b\u4f3c\uff0c<code>filter()<\/code>\u4e5f\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u5e8f\u5217\u3002\u548c<code>map()<\/code>\u4e0d\u540c\u7684\u662f\uff0c<code>filter()<\/code>\u628a\u4f20\u5165\u7684\u51fd\u6570\u4f9d\u6b21\u4f5c\u7528\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u6839\u636e\u8fd4\u56de\u503c\u662f<code>True<\/code>\u8fd8\u662f<code>False<\/code>\u51b3\u5b9a\u4fdd\u7559\u8fd8\u662f\u4e22\u5f03\u8be5\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5728\u4e00\u4e2alist\u4e2d\uff0c\u5220\u6389\u5076\u6570\uff0c\u53ea\u4fdd\u7559\u5947\u6570\uff0c\u53ef\u4ee5\u8fd9\u4e48\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def is_odd(n):\n    return n % 2 == 1\n\nlist(filter(is_odd, &#91;1, 2, 4, 5, 6, 9, 10, 15]))\n# \u7ed3\u679c: &#91;1, 5, 9, 15]\n<\/code><\/pre>\n\n\n\n<p>\u628a\u4e00\u4e2a\u5e8f\u5217\u4e2d\u7684\u7a7a\u5b57\u7b26\u4e32\u5220\u6389\uff0c\u53ef\u4ee5\u8fd9\u4e48\u5199\uff1a<\/p>\n\n\n\n<p><strong>def<\/strong> <strong>not_empty<\/strong>(s):<br>    <strong>return<\/strong> s <strong>and<\/strong> s.strip()<br><br>list(filter(not_empty, [&#8216;A&#8217;, &#8221;, &#8216;B&#8217;, None, &#8216;C&#8217;, &#8216;  &#8216;]))<br><em># \u7ed3\u679c: [&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;]<\/em><\/p>\n\n\n\n<p>s and s.strip()\u201c \u8fd9\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u3002Python\u8bed\u6cd5\u662f\u8fd9\u4e48\u8fd0\u884c\u7684\uff1a\u5982\u679cs is None;\u90a3\u4e48s\u4f1a\u88ab\u5224\u65ad\u4e3aFalse\u3002\u800cFalse\u4e0d\u7ba1\u548c\u4ec0\u4e48\u505aand;\u7ed3\u679c\u90fd\u662fFalse;\u6240\u4ee5\u4e0d\u9700\u8981\u770band\u540e\u9762\u7684\u8868\u8fbe\u5f0f;\u76f4\u63a5\u8fd4\u56de;\u6ce8\u610f\u4e0d\u662f\u8fd4\u56deFalse;\u3002\u5982\u679cs is not None;\u90a3\u4e48s\u4f1a\u88ab\u5224\u65ad\u4e3aTrue;\u800cTrue\u4e0d\u7ba1\u548c\u4ec0\u4e48and\u90fd\u8fd4\u56de\u540e\u4e00\u9879\u3002\u4e8e\u662f\u5c31\u8fd4\u56de\u4e86s.strip()\u3002\u6240\u4ee5s.strip() \u4e0d\u80fd\u5355\u72ec\u4f7f\u7528;\u8bed\u6cd5\u662f\u6709\u95ee\u9898\u7684\u3002<\/p>\n\n\n\n<p>\u53ef\u89c1\u7528<code>filter()<\/code>\u8fd9\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u5173\u952e\u5728\u4e8e\u6b63\u786e\u5b9e\u73b0\u4e00\u4e2a\u201c\u7b5b\u9009\u201d\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\u5230<code>filter()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a<code>Iterator<\/code>\uff0c\u4e5f\u5c31\u662f\u4e00\u4e2a\u60f0\u6027\u5e8f\u5217\uff0c\u6240\u4ee5\u8981\u5f3a\u8feb<code>filter()<\/code>\u5b8c\u6210\u8ba1\u7b97\u7ed3\u679c\uff0c\u9700\u8981\u7528<code>list()<\/code>\u51fd\u6570\u83b7\u5f97\u6240\u6709\u7ed3\u679c\u5e76\u8fd4\u56delist\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\">\u4e03\u3001<\/p>\n\n\n\n<p>\u9ad8\u9636\u51fd\u6570\u9664\u4e86\u53ef\u4ee5\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u628a\u51fd\u6570\u4f5c\u4e3a\u7ed3\u679c\u503c\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u6765\u5b9e\u73b0\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570\u7684\u6c42\u548c\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6c42\u548c\u7684\u51fd\u6570\u662f\u8fd9\u6837\u5b9a\u4e49\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calc_sum(*args):\n    ax = 0\n    for n in args:\n        ax = ax + n\n    return ax\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\uff0c\u5982\u679c\u4e0d\u9700\u8981\u7acb\u523b\u6c42\u548c\uff0c\u800c\u662f\u5728\u540e\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6839\u636e\u9700\u8981\u518d\u8ba1\u7b97\u600e\u4e48\u529e\uff1f\u53ef\u4ee5\u4e0d\u8fd4\u56de\u6c42\u548c\u7684\u7ed3\u679c\uff0c\u800c\u662f\u8fd4\u56de\u6c42\u548c\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def<\/strong> <strong>lazy_sum<\/strong>(*args):\n    <strong>def<\/strong> <strong>sum<\/strong>():\n        ax = 0\n        <strong>for<\/strong> n <strong>in<\/strong> args:\n            ax = ax + n\n        <strong>return<\/strong> ax\n    <strong>return<\/strong> sum\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u8c03\u7528<code>lazy_sum()<\/code>\u65f6\uff0c\u8fd4\u56de\u7684\u5e76\u4e0d\u662f\u6c42\u548c\u7ed3\u679c\uff0c\u800c\u662f\u6c42\u548c\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; f = lazy_sum(1, 3, 5, 7, 9)\n&gt;&gt;&gt; f\n&lt;<strong>function<\/strong> lazy_sum.&lt;locals&gt;.sum at 0x101c6ed90&gt;\n<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\u51fd\u6570<code>f<\/code>\u65f6\uff0c\u624d\u771f\u6b63\u8ba1\u7b97\u6c42\u548c\u7684\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; f()\n25\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u51fd\u6570<code>lazy_sum<\/code>\u4e2d\u53c8\u5b9a\u4e49\u4e86\u51fd\u6570<code>sum<\/code>\uff0c\u5e76\u4e14\uff0c\u5185\u90e8\u51fd\u6570<code>sum<\/code>\u53ef\u4ee5\u5f15\u7528\u5916\u90e8\u51fd\u6570<code>lazy_sum<\/code>\u7684\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\uff0c\u5f53<code>lazy_sum<\/code>\u8fd4\u56de\u51fd\u6570<code>sum<\/code>\u65f6\uff0c\u76f8\u5173\u53c2\u6570\u548c\u53d8\u91cf\u90fd\u4fdd\u5b58\u5728\u8fd4\u56de\u7684\u51fd\u6570\u4e2d\uff0c\u8fd9\u79cd\u79f0\u4e3a\u201c\u95ed\u5305\uff08Closure\uff09\u201d\u7684\u7a0b\u5e8f\u7ed3\u6784\u62e5\u6709\u6781\u5927\u7684\u5a01\u529b\u3002<\/p>\n\n\n\n<p>\u8bf7\u518d\u6ce8\u610f\u4e00\u70b9\uff0c\u5f53\u6211\u4eec\u8c03\u7528<code>lazy_sum()<\/code>\u65f6\uff0c<strong>\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570<\/strong>\uff0c\u5373\u4f7f\u4f20\u5165\u76f8\u540c\u7684\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; f1 = lazy_sum(1, 3, 5, 7, 9)\n&gt;&gt;&gt; f2 = lazy_sum(1, 3, 5, 7, 9)\n&gt;&gt;&gt; f1==f2\nFalse\n<\/code><\/pre>\n\n\n\n<p><code>f1()<\/code>\u548c<code>f2()<\/code>\u7684\u8c03\u7528\u7ed3\u679c\u4e92\u4e0d\u5f71\u54cd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">         \u95ed\u5305<\/h3>\n\n\n\n<p>\u6ce8\u610f\u5230\u8fd4\u56de\u7684\u51fd\u6570\u5728\u5176\u5b9a\u4e49\u5185\u90e8\u5f15\u7528\u4e86\u5c40\u90e8\u53d8\u91cf<code>args<\/code>\uff0c\u6240\u4ee5\uff0c\u5f53\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u51fd\u6570\u540e\uff0c\u5176\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\u8fd8\u88ab\u65b0\u51fd\u6570\u5f15\u7528\uff0c\u6240\u4ee5\uff0c\u95ed\u5305\u7528\u8d77\u6765\u7b80\u5355\uff0c\u5b9e\u73b0\u8d77\u6765\u53ef\u4e0d\u5bb9\u6613\u3002<\/p>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u95ee\u9898\u662f\uff0c<strong>\u8fd4\u56de\u7684\u51fd\u6570\u5e76\u6ca1\u6709\u7acb\u523b\u6267\u884c\uff0c\u800c\u662f\u76f4\u5230\u8c03\u7528\u4e86<code>f()<\/code>\u624d\u6267\u884c<\/strong>\u3002\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def<\/strong> <strong>count<\/strong>():\n    fs = &#91;]\n    <strong>for<\/strong> i <strong>in<\/strong> range(1, 4):\n        <strong>def<\/strong> <strong>f<\/strong>():\n             <strong>return<\/strong> i*i\n        fs.append(f)\n    <strong>return<\/strong> fs\n\nf1, f2, f3 = count()\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u6b21\u5faa\u73af\uff0c\u90fd\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u7136\u540e\uff0c\u628a\u521b\u5efa\u76843\u4e2a\u51fd\u6570\u90fd\u8fd4\u56de\u4e86\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u80fd\u8ba4\u4e3a\u8c03\u7528<code>f1()<\/code>\uff0c<code>f2()<\/code>\u548c<code>f3()<\/code>\u7ed3\u679c\u5e94\u8be5\u662f<code>1<\/code>\uff0c<code>4<\/code>\uff0c<code>9<\/code>\uff0c\u4f46\u5b9e\u9645\u7ed3\u679c\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; f1()\n9\n&gt;&gt;&gt; f2()\n9\n&gt;&gt;&gt; f3()\n9\n<\/code><\/pre>\n\n\n\n<p>\u5168\u90e8\u90fd\u662f<code>9<\/code>\uff01\u539f\u56e0\u5c31\u5728\u4e8e\u8fd4\u56de\u7684\u51fd\u6570\u5f15\u7528\u4e86\u53d8\u91cf<code>i<\/code>\uff0c\u4f46\u5b83\u5e76\u975e\u7acb\u523b\u6267\u884c\u3002\u7b49\u52303\u4e2a\u51fd\u6570\u90fd\u8fd4\u56de\u65f6\uff0c\u5b83\u4eec\u6240\u5f15\u7528\u7684\u53d8\u91cf<code>i<\/code>\u5df2\u7ecf\u53d8\u6210\u4e86<code>3<\/code>\uff0c\u56e0\u6b64\u6700\u7ec8\u7ed3\u679c\u4e3a<code>9<\/code>\u3002&nbsp;\u8fd4\u56de\u95ed\u5305\u65f6\u7262\u8bb0\u4e00\u70b9\uff1a\u8fd4\u56de\u51fd\u6570\u4e0d\u8981\u5f15\u7528\u4efb\u4f55\u5faa\u73af\u53d8\u91cf\uff0c\u6216\u8005\u540e\u7eed\u4f1a\u53d1\u751f\u53d8\u5316\u7684\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e00\u5b9a\u8981\u5f15\u7528\u5faa\u73af\u53d8\u91cf\u600e\u4e48\u529e\uff1f\u65b9\u6cd5\u662f\u518d\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u8be5\u51fd\u6570\u7684\u53c2\u6570\u7ed1\u5b9a\u5faa\u73af\u53d8\u91cf\u5f53\u524d\u7684\u503c\uff0c\u65e0\u8bba\u8be5\u5faa\u73af\u53d8\u91cf\u540e\u7eed\u5982\u4f55\u66f4\u6539\uff0c\u5df2\u7ed1\u5b9a\u5230\u51fd\u6570\u53c2\u6570\u7684\u503c\u4e0d\u53d8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def<\/strong> <strong>count<\/strong>():\n    <strong>def<\/strong> <strong>f<\/strong>(j):\n        <strong>def<\/strong> <strong>g<\/strong>():\n            <strong>return<\/strong> j*j\n        <strong>return<\/strong> g\n    fs = &#91;]\n    <strong>for<\/strong> i <strong>in<\/strong> range(1, 4):\n        fs.append(f(i)) <em># f(i)\u7acb\u523b\u88ab\u6267\u884c\uff0c\u56e0\u6b64i\u7684\u5f53\u524d\u503c\u88ab\u4f20\u5165f()<\/em>\n    <strong>return<\/strong> fs\n<\/code><\/pre>\n\n\n\n<p>\u518d\u770b\u770b\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; f1, f2, f3 = count()\n&gt;&gt;&gt; f1()\n1\n&gt;&gt;&gt; f2()\n4\n&gt;&gt;&gt; f3()\n9\n<\/code><\/pre>\n\n\n\n<p>\u7f3a\u70b9\u662f\u4ee3\u7801\u8f83\u957f\uff0c\u53ef\u5229\u7528lambda\u51fd\u6570\u7f29\u77ed\u4ee3\u7801<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">nonlocal<\/h3>\n\n\n\n<p>\u4f7f\u7528\u95ed\u5305\uff0c\u5c31\u662f\u5185\u5c42\u51fd\u6570\u5f15\u7528\u4e86\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u5982\u679c\u53ea\u662f\u8bfb\u5916\u5c42\u53d8\u91cf\u7684\u503c\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u8fd4\u56de\u7684\u95ed\u5305\u51fd\u6570\u8c03\u7528\u4e00\u5207\u6b63\u5e38\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def inc():\n    x = 0\n    def fn():\n        # \u4ec5\u8bfb\u53d6x\u7684\u503c:\n        return x + 1\n    return fn\n\nf = inc()\nprint(f()) # 1\nprint(f()) # 1\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\uff0c\u5982\u679c\u5bf9\u5916\u5c42\u53d8\u91cf\u8d4b\u503c\uff0c\u7531\u4e8ePython\u89e3\u91ca\u5668\u4f1a\u628a<code>x<\/code>\u5f53\u4f5c\u51fd\u6570<code>fn()<\/code>\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5b83\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def inc():\n    x = 0\n    def fn():\n        # nonlocal x\n        x = x + 1\n        return x\n    return fn\n\nf = inc()\nprint(f()) # 1\nprint(f()) # 2\n<\/code><\/pre>\n\n\n\n<p>\u539f\u56e0\u662f<code>x<\/code>\u4f5c\u4e3a\u5c40\u90e8\u53d8\u91cf\u5e76\u6ca1\u6709\u521d\u59cb\u5316\uff0c\u76f4\u63a5\u8ba1\u7b97<code>x+1<\/code>\u662f\u4e0d\u884c\u7684\u3002\u4f46\u6211\u4eec\u5176\u5b9e\u662f\u60f3\u5f15\u7528<code>inc()<\/code>\u51fd\u6570\u5185\u90e8\u7684<code>x<\/code>\uff0c\u6240\u4ee5\u9700\u8981\u5728<code>fn()<\/code>\u51fd\u6570\u5185\u90e8\u52a0\u4e00\u4e2a<code>nonlocal x<\/code>\u7684\u58f0\u660e\u3002\u52a0\u4e0a\u8fd9\u4e2a\u58f0\u660e\u540e\uff0c\u89e3\u91ca\u5668\u628a<code>fn()<\/code>\u7684<code>x<\/code>\u770b\u4f5c\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5b83\u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u4e86\uff0c\u53ef\u4ee5\u6b63\u786e\u8ba1\u7b97<code>x+1<\/code>\u3002<\/p>\n\n\n\n<p>&nbsp;\u63d0\u793a<\/p>\n\n\n\n<p>\u4f7f\u7528\u95ed\u5305\u65f6\uff0c\u5bf9\u5916\u5c42\u53d8\u91cf\u8d4b\u503c\u524d\uff0c\u9700\u8981\u5148\u4f7f\u7528nonlocal\u58f0\u660e\u8be5\u53d8\u91cf\u4e0d\u662f\u5f53\u524d\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\">\u516b\u3001\u88c5\u9970\u5668Decorator<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\u7531\u4e8e\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e14\u51fd\u6570\u5bf9\u8c61\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u6240\u4ee5\uff0c\u901a\u8fc7\u53d8\u91cf\u4e5f\u80fd\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; def now():\n...     print('2024-6-1')\n...\n&gt;&gt;&gt; f = now\n&gt;&gt;&gt; f()\n2024-6-1\n<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570\u5bf9\u8c61\u6709\u4e00\u4e2a<code>__name__<\/code>\u5c5e\u6027\uff08\u6ce8\u610f\uff1a\u662f\u524d\u540e\u5404\u4e24\u4e2a\u4e0b\u5212\u7ebf\uff09\uff0c\u53ef\u4ee5\u62ff\u5230\u51fd\u6570\u7684\u540d\u5b57\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; now.__name__\n'now'\n&gt;&gt;&gt; f.__name__\n'now'\n<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u589e\u5f3a<code>now()<\/code>\u51fd\u6570\u7684\u529f\u80fd\uff0c\u6bd4\u5982\uff0c\u5728\u51fd\u6570\u8c03\u7528\u524d\u540e\u81ea\u52a8\u6253\u5370\u65e5\u5fd7\uff0c\u4f46\u53c8\u4e0d\u5e0c\u671b\u4fee\u6539<code>now()<\/code>\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u8fd9\u79cd\u5728\u4ee3\u7801\u8fd0\u884c\u671f\u95f4\u52a8\u6001\u589e\u52a0\u529f\u80fd\u7684\u65b9\u5f0f\uff0c\u79f0\u4e4b\u4e3a\u201c\u88c5\u9970\u5668\u201d\uff08Decorator\uff09\u3002<\/p>\n\n\n\n<p>\u672c\u8d28\u4e0a\uff0cdecorator\u5c31\u662f\u4e00\u4e2a\u8fd4\u56de\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u8981\u5b9a\u4e49\u4e00\u4e2a\u80fd\u6253\u5370\u65e5\u5fd7\u7684decorator\uff0c\u53ef\u4ee5\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def log(func):\n    def wrapper(*args, **kw):\n        print('call %s():' % func.__name__)\n        return func(*args, **kw)\n    return wrapper\n<\/code><\/pre>\n\n\n\n<p>\u89c2\u5bdf\u4e0a\u9762\u7684<code>log<\/code>\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u4e2adecorator\uff0c\u6240\u4ee5\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u8981\u501f\u52a9Python\u7684@\u8bed\u6cd5\uff0c\u628adecorator\u7f6e\u4e8e\u51fd\u6570\u7684\u5b9a\u4e49\u5904\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@log\ndef now():\n    print('2024-6-1')\n<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528<code>now()<\/code>\u51fd\u6570\uff0c\u4e0d\u4ec5\u4f1a\u8fd0\u884c<code>now()<\/code>\u51fd\u6570\u672c\u8eab\uff0c\u8fd8\u4f1a\u5728\u8fd0\u884c<code>now()<\/code>\u51fd\u6570\u524d\u6253\u5370\u4e00\u884c\u65e5\u5fd7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; now()\ncall now():\n2024-6-1\n<\/code><\/pre>\n\n\n\n<p>\u628a<code>@log<\/code>\u653e\u5230<code>now()<\/code>\u51fd\u6570\u7684\u5b9a\u4e49\u5904\uff0c\u76f8\u5f53\u4e8e\u6267\u884c\u4e86\u8bed\u53e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>now = log(now)\n<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8e<code>log()<\/code>\u662f\u4e00\u4e2adecorator\uff0c\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\uff0c\u6240\u4ee5\uff0c\u539f\u6765\u7684<code>now()<\/code>\u51fd\u6570\u4ecd\u7136\u5b58\u5728\uff0c\u53ea\u662f\u73b0\u5728\u540c\u540d\u7684<code>now<\/code>\u53d8\u91cf\u6307\u5411\u4e86\u65b0\u7684\u51fd\u6570\uff0c\u4e8e\u662f\u8c03\u7528<code>now()<\/code>\u5c06\u6267\u884c\u65b0\u51fd\u6570\uff0c\u5373\u5728<code>log()<\/code>\u51fd\u6570\u4e2d\u8fd4\u56de\u7684<code>wrapper()<\/code>\u51fd\u6570\u3002<\/p>\n\n\n\n<p><code>wrapper()<\/code>\u51fd\u6570\u7684\u53c2\u6570\u5b9a\u4e49\u662f<code>(*args, **kw)<\/code>\uff0c\u56e0\u6b64\uff0c<code>wrapper()<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u53c2\u6570\u7684\u8c03\u7528\u3002\u5728<code>wrapper()<\/code>\u51fd\u6570\u5185\uff0c\u9996\u5148\u6253\u5370\u65e5\u5fd7\uff0c\u518d\u7d27\u63a5\u7740\u8c03\u7528\u539f\u59cb\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u5982\u679cdecorator\u672c\u8eab\u9700\u8981\u4f20\u5165\u53c2\u6570\uff0c\u90a3\u5c31\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u8fd4\u56dedecorator\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u5199\u51fa\u6765\u4f1a\u66f4\u590d\u6742\u3002\u6bd4\u5982\uff0c\u8981\u81ea\u5b9a\u4e49log\u7684\u6587\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def log(text):\n    def decorator(func):\n        def wrapper(*args, **kw):\n            print('%s %s():' % (text, func.__name__))\n            return func(*args, **kw)\n        return wrapper\n    return decorator\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a3\u5c42\u5d4c\u5957\u7684decorator\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@log('execute')\ndef now():\n    print('2024-6-1')\n<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; now()\nexecute now():\n2024-6-1\n<\/code><\/pre>\n\n\n\n<p>\u548c\u4e24\u5c42\u5d4c\u5957\u7684decorator\u76f8\u6bd4\uff0c3\u5c42\u5d4c\u5957\u7684\u6548\u679c\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; now = log('execute')(now)\n<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u6765\u5256\u6790\u4e0a\u9762\u7684\u8bed\u53e5\uff0c\u9996\u5148\u6267\u884c<code>log('execute')<\/code>\uff0c\u8fd4\u56de\u7684\u662f<code>decorator<\/code>\u51fd\u6570\uff0c\u518d\u8c03\u7528\u8fd4\u56de\u7684\u51fd\u6570\uff0c\u53c2\u6570\u662f<code>now<\/code>\u51fd\u6570\uff0c\u8fd4\u56de\u503c\u6700\u7ec8\u662f<code>wrapper<\/code>\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u4e24\u79cddecorator\u7684\u5b9a\u4e49\u90fd\u6ca1\u6709\u95ee\u9898\uff0c\u4f46\u8fd8\u5dee\u6700\u540e\u4e00\u6b65\u3002\u56e0\u4e3a\u6211\u4eec\u8bb2\u4e86\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff0c\u5b83\u6709<code>__name__<\/code>\u7b49\u5c5e\u6027\uff0c\u4f46\u4f60\u53bb\u770b\u7ecf\u8fc7decorator\u88c5\u9970\u4e4b\u540e\u7684\u51fd\u6570\uff0c\u5b83\u4eec\u7684<code>__name__<\/code>\u5df2\u7ecf\u4ece\u539f\u6765\u7684<code>'now'<\/code>\u53d8\u6210\u4e86<code>'wrapper'<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; now.__name__\n'wrapper'\n<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3a\u8fd4\u56de\u7684\u90a3\u4e2a<code>wrapper()<\/code>\u51fd\u6570\u540d\u5b57\u5c31\u662f<code>'wrapper'<\/code>\uff0c\u6240\u4ee5\uff0c\u9700\u8981\u628a\u539f\u59cb\u51fd\u6570\u7684<code>__name__<\/code>\u7b49\u5c5e\u6027\u590d\u5236\u5230<code>wrapper()<\/code>\u51fd\u6570\u4e2d\uff0c\u5426\u5219\uff0c\u6709\u4e9b\u4f9d\u8d56\u51fd\u6570\u7b7e\u540d\u7684\u4ee3\u7801\u6267\u884c\u5c31\u4f1a\u51fa\u9519\u3002<\/p>\n\n\n\n<p>\u4e0d\u9700\u8981\u7f16\u5199<code>wrapper.__name__ = func.__name__<\/code>\u8fd9\u6837\u7684\u4ee3\u7801\uff0cPython\u5185\u7f6e\u7684<code>functools.wraps<\/code>\u5c31\u662f\u5e72\u8fd9\u4e2a\u4e8b\u7684\uff0c\u6240\u4ee5\uff0c\u4e00\u4e2a\u5b8c\u6574\u7684decorator\u7684\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import functools\n\ndef log(func):\n    @functools.wraps(func)\n    def wrapper(*args, **kw):\n        print('call %s():' % func.__name__)\n        return func(*args, **kw)\n    return wrapper\n<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\u9488\u5bf9\u5e26\u53c2\u6570\u7684decorator\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import functools\n\ndef log(text):\n    def decorator(func):\n        @functools.wraps(func)\n        def wrapper(*args, **kw):\n            print('%s %s():' % (text, func.__name__))\n            return func(*args, **kw)\n        return wrapper\n    return decorator\n<\/code><\/pre>\n\n\n\n<p><code>import functools<\/code>\u662f\u5bfc\u5165<code>functools<\/code>\u6a21\u5757\u3002\u6a21\u5757\u7684\u6982\u5ff5\u7a0d\u5019\u8bb2\u89e3\u3002\u73b0\u5728\uff0c\u53ea\u9700\u8bb0\u4f4f\u5728\u5b9a\u4e49<code>wrapper()<\/code>\u7684\u524d\u9762\u52a0\u4e0a<code>@functools.wraps(func)<\/code>\u5373\u53ef<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u4e5d\u3001<\/strong><\/p>\n\n\n\n<p>mylist=[&#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;,&#8221;a&#8221;,&#8221;b&#8221;]      #\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n\n\n\n<p>print(mylist.index(&#8220;c&#8221;,0,4))      #\u67e5\u627e\u6307\u5b9a\u5217\u8868\u4e0b\u6807\u8303\u56f4\u7684\u5143\u7d20&#8221;c&#8221;\uff0c\u5e76\u8fd4\u56de\u627e\u5230\u5bf9\u5e94\u6570\u636e\u7684\u4e0b\u6807\uff0c\u8303\u56f4\u533a\u95f4\u662f<strong>\u5de6\u95ed\u53f3\u5f00<\/strong>\u30100\uff0c4\uff09\uff0c\u4e5f\u5c31\u662f\u5305\u542b0\u4f46\u4e0d\u5305\u542b4\uff0c<\/p>\n\n\n\n<p>\u6b64\u5904\u663e\u793a\u7684\u7ed3\u679c\u4e3a  2<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>tup1=(&#8220;aa&#8221;,&#8221;bb&#8221;,22,&#8221;dd&#8221;,&#8221;ee&#8221;,&#8221;ff&#8221;)<\/p>\n\n\n\n<p>print(tup1[1:4])              #  \u5bf9\u5143\u7ec4\u8fdb\u884c\u5207\u7247\uff0c\u4e5f\u662f\u5de6\u95ed\u53f3\u5f00,\u7ed3\u679c\u4e3a(&#8220;bb&#8221;,22,&#8221;dd&#8221;)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4e00\u4e2a0-99\u7684\u6570\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u524d10\u4e2a\u6570\uff0c\u6bcf\u4e24\u4e2a\u53d6\u4e00\u4e2a\uff1a\n&gt;&gt;&gt; L&#91;:10:2]\n&#91;0, 2, 4, 6, 8]\n\u540e10\u4e2a\u6570\n&gt;&gt;&gt; L&#91;-10:]\n&#91;90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n\n<\/code><\/pre>\n\n\n\n<p><strong>\u5341\u3001<\/strong><\/p>\n\n\n\n<p><code>continue<\/code>\u8bed\u53e5\uff0c\u8df3\u8fc7\u5f53\u524d\u7684\u8fd9\u6b21\u5faa\u73af\uff0c\u76f4\u63a5\u5f00\u59cb\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n\n\n\n<p><code>break<\/code>\u8bed\u53e5\u53ef\u4ee5\u63d0\u524d\u9000\u51fa\u5f53\u524d\u5faa\u73af\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u5341\u4e00\u3001\u5b57\u5178<\/strong><\/p>\n\n\n\n<p>#\u7528\u5927\u62ec\u53f7\u5b9a\u4e49\u5b57\u5178\uff0c\u5176\u5b9e\u5c31\u662f\u591a\u4e2a\u952e-\u503c(key:value)\u5bf9\uff0c\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\u4e5f\u79f0\u4e3amap\uff0c<\/p>\n\n\n\n<p>info={&#8220;name&#8221;:&#8221;\u5218\u5fb7\u534e&#8221;,&#8221;age&#8221;:56,&#8221;sex&#8221;:&#8221;man&#8221;}    <\/p>\n\n\n\n<p>#\u7528\u4e2d\u62ec\u53f7\u91cc\u5199KEY\u503c\u8fdb\u884c\u5b57\u5178\u7684value\u503c\u7684\u8bbf\u95ee,\u7ed3\u679c\u4e3a\u201c\u5218\u5fb7\u534e\u201d<\/p>\n\n\n\n<p>print(info[&#8220;name&#8221;])<\/p>\n\n\n\n<p>#\u5982\u679c\u7528print(info[&#8220;gender&#8221;])\u76f4\u63a5\u67e5\u627e\uff0c\u56e0\u4e3a\u6ca1\u6709\u8fd9\u4e2a\u952e\uff0c\u6240\u4ee5\u4f1a\u62a5\u9519\uff0c\u6211\u4eec\u7528\u4ee5\u4e0bget\u65b9\u6cd5\u6765\u67e5\u627e\uff0c\u540e\u9762\u7684&#8221;m&#8221;\u4e3a\u6ca1\u627e\u5230\u94fe\u7684\u65f6\u5019\uff0c\u8bbe\u7f6e\u6210\u7684\u53ef\u4ee5\u8fd4\u56de\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n\n\n\n<p>print(info.get(&#8220;gender&#8221;,&#8221;m&#8221;))<\/p>\n\n\n\n<p>#\u5220\u9664\u6307\u5b9a\u7684\u952e-\u503c(key:value)\u5bf9\uff0c\u4e0d\u7528\u65b9\u6cd5\uff0c\u76f4\u63a5\u7528del,\u4e0b\u9762\u628a &#8220;name&#8221;:&#8221;\u5218\u5fb7\u534e&#8221;  \u8fd9\u4e2a\u952e-\u503c(key:value)\u5bf9\u5220\u9664<\/p>\n\n\n\n<p>del info[&#8220;name&#8221;]<\/p>\n\n\n\n<p>dict\u5185\u90e8\u5b58\u653e\u7684\u987a\u5e8f\u548ckey\u653e\u5165\u7684\u987a\u5e8f\u662f\u6ca1\u6709\u5173\u7cfb\u7684\u3002<\/p>\n\n\n\n<p>\u6b63\u786e\u4f7f\u7528dict\u975e\u5e38\u91cd\u8981\uff0c\u9700\u8981\u7262\u8bb0\u7684\u7b2c\u4e00\u6761\u5c31\u662fdict\u7684key\u5fc5\u987b\u662f<strong>\u4e0d\u53ef\u53d8\u5bf9\u8c61<\/strong>\u3002<\/p>\n\n\n\n<p>set\u548cdict\u7c7b\u4f3c\uff0c\u4e5f\u662f\u4e00\u7ec4key\u7684\u96c6\u5408\uff0c\u4f46\u4e0d\u5b58\u50a8value\u3002\u7531\u4e8ekey\u4e0d\u80fd\u91cd\u590d\uff0c\u6240\u4ee5\uff0c\u5728set\u4e2d\uff0c\u6ca1\u6709\u91cd\u590d\u7684key\u3002<\/p>\n\n\n\n<p>\u8981\u521b\u5efa\u4e00\u4e2aset\uff0c\u7528<code>{x,y,z,...}<\/code>\u5217\u51fa\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; s = {1, 2, 3}\n&gt;&gt;&gt; s\n{1, 2, 3}\n<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\u63d0\u4f9b\u4e00\u4e2alist\u4f5c\u4e3a\u8f93\u5165\u96c6\u5408\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; s = set(&#91;1, 2, 3])\n&gt;&gt;&gt; s\n{1, 2, 3}\n<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\uff0c\u4f20\u5165\u7684\u53c2\u6570<code>[1, 2, 3]<\/code>\u662f\u4e00\u4e2alist\uff0c\u800c\u663e\u793a\u7684<code>{1, 2, 3}<\/code>\u53ea\u662f\u544a\u8bc9\u4f60\u8fd9\u4e2aset\u5185\u90e8\u67091\uff0c2\uff0c3\u8fd93\u4e2a\u5143\u7d20\uff0c\u663e\u793a\u7684\u987a\u5e8f\u4e5f\u4e0d\u8868\u793aset\u662f\u6709\u5e8f\u7684\u3002\u3002<\/p>\n\n\n\n<p>\u91cd\u590d\u5143\u7d20\u5728set\u4e2d\u81ea\u52a8\u88ab\u8fc7\u6ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; s = {1, 1, 2, 2, 3, 3}\n&gt;&gt;&gt; s\n{1, 2, 3}\n<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7<code>add(key)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6dfb\u52a0\u5143\u7d20\u5230set\u4e2d\uff0c\u53ef\u4ee5\u91cd\u590d\u6dfb\u52a0\uff0c\u4f46\u4e0d\u4f1a\u6709\u6548\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; s.add(4)\n&gt;&gt;&gt; s\n{1, 2, 3, 4}\n&gt;&gt;&gt; s.add(4)\n&gt;&gt;&gt; s\n{1, 2, 3, 4}\n<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7<code>remove(key)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; s.remove(4)\n&gt;&gt;&gt; s\n{1, 2, 3}\n<\/code><\/pre>\n\n\n\n<p>set\u53ef\u4ee5\u770b\u6210\u6570\u5b66\u610f\u4e49\u4e0a\u7684\u65e0\u5e8f\u548c\u65e0\u91cd\u590d\u5143\u7d20\u7684\u96c6\u5408\uff0c\u56e0\u6b64\uff0c\u4e24\u4e2aset\u53ef\u4ee5\u505a\u6570\u5b66\u610f\u4e49\u4e0a\u7684\u4ea4\u96c6\u3001\u5e76\u96c6\u7b49\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; s1 = {1, 2, 3}\n&gt;&gt;&gt; s2 = {2, 3, 4}\n&gt;&gt;&gt; s1 &amp; s2\n{2, 3}\n&gt;&gt;&gt; s1 | s2\n{1, 2, 3, 4}\n<\/code><\/pre>\n\n\n\n<p>set\u548cdict\u7684\u552f\u4e00\u533a\u522b\u4ec5\u5728\u4e8e\u6ca1\u6709\u5b58\u50a8\u5bf9\u5e94\u7684value\uff0c\u4f46\u662f\uff0cset\u7684\u539f\u7406\u548cdict\u4e00\u6837\uff0c\u6240\u4ee5\uff0c\u540c\u6837\u4e0d\u53ef\u4ee5\u653e\u5165\u53ef\u53d8\u5bf9\u8c61\uff0c\u56e0\u4e3a\u65e0\u6cd5\u5224\u65ad\u4e24\u4e2a\u53ef\u53d8\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\uff0c\u4e5f\u5c31\u65e0\u6cd5\u4fdd\u8bc1set\u5185\u90e8\u201c\u4e0d\u4f1a\u6709\u91cd\u590d\u5143\u7d20\u201d\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5341\u4e8c\u3001<\/p>\n\n\n\n<p>\u5217\u8868\u751f\u6210\u5f0f\u5373List Comprehensions\uff0c\u662fPython\u5185\u7f6e\u7684\u975e\u5e38\u7b80\u5355\u5374\u5f3a\u5927\u7684\u53ef\u4ee5\u7528\u6765\u521b\u5efalist\u7684\u751f\u6210\u5f0f\u3002<\/p>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u8981\u751f\u6210list&nbsp;<code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/code>\u53ef\u4ee5\u7528<code>list(range(1, 11))<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; list(range(1, 11))\n&#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u5982\u679c\u8981\u751f\u6210<code>[1x1, 2x2, 3x3, ..., 10x10]<\/code>\u600e\u4e48\u505a\uff1f\u65b9\u6cd5\u4e00\u662f\u5faa\u73af\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; L = &#91;]\n&gt;&gt;&gt; for x in range(1, 11):\n...    L.append(x * x)\n...\n&gt;&gt;&gt; L\n&#91;1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u5faa\u73af\u592a\u7e41\u7410\uff0c\u800c\u5217\u8868\u751f\u6210\u5f0f\u5219\u53ef\u4ee5\u7528\u4e00\u884c\u8bed\u53e5\u4ee3\u66ff\u5faa\u73af\u751f\u6210\u4e0a\u9762\u7684list\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; &#91;x * x for x in range(1, 11)]\n&#91;1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n<\/code><\/pre>\n\n\n\n<p>\u5199\u5217\u8868\u751f\u6210\u5f0f\u65f6\uff0c\u628a\u8981\u751f\u6210\u7684\u5143\u7d20<code>x * x<\/code>\u653e\u5230\u524d\u9762\uff0c\u540e\u9762\u8ddf<code>for<\/code>\u5faa\u73af\uff0c\u5c31\u53ef\u4ee5\u628alist\u521b\u5efa\u51fa\u6765\uff0c\u5341\u5206\u6709\u7528\uff0c\u591a\u5199\u51e0\u6b21\uff0c\u5f88\u5feb\u5c31\u53ef\u4ee5\u719f\u6089\u8fd9\u79cd\u8bed\u6cd5\u3002<\/p>\n\n\n\n<p>for\u5faa\u73af\u540e\u9762\u8fd8\u53ef\u4ee5\u52a0\u4e0aif\u5224\u65ad\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u7b5b\u9009\u51fa\u4ec5\u5076\u6570\u7684\u5e73\u65b9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; &#91;x * x for x in range(1, 11) if x % 2 == 0]\n&#91;4, 16, 36, 64, 100]\n<\/code><\/pre>\n\n\n\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e24\u5c42\u5faa\u73af\uff0c\u53ef\u4ee5\u751f\u6210\u5168\u6392\u5217\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; &#91;m + n for m in 'ABC' for n in 'XYZ']\n&#91;'AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']\n<\/code><\/pre>\n\n\n\n<p>\u4e09\u5c42\u548c\u4e09\u5c42\u4ee5\u4e0a\u7684\u5faa\u73af\u5c31\u5f88\u5c11\u7528\u5230\u4e86\u3002<\/p>\n\n\n\n<p>\u8fd0\u7528\u5217\u8868\u751f\u6210\u5f0f\uff0c\u53ef\u4ee5\u5199\u51fa\u975e\u5e38\u7b80\u6d01\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u540d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; import os # \u5bfc\u5165os\u6a21\u5757\uff0c\u6a21\u5757\u7684\u6982\u5ff5\u540e\u9762\u8bb2\u5230\n&gt;&gt;&gt; &#91;d for d in os.listdir('.')] # os.listdir\u53ef\u4ee5\u5217\u51fa\u6587\u4ef6\u548c\u76ee\u5f55<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u5341\u4e09<\/strong>  \u751f\u6210\u5668<\/p>\n\n\n\n<p>\u6458\u81ea<a href=\"https:\/\/liaoxuefeng.com\/books\/python\/advanced\/generator\/index.html\">https:\/\/liaoxuefeng.com\/books\/python\/advanced\/generator\/index.html<\/a><\/p>\n\n\n\n<p>\u901a\u8fc7\u5217\u8868\u751f\u6210\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u3002\u4f46\u662f\uff0c\u53d7\u5230\u5185\u5b58\u9650\u5236\uff0c\u5217\u8868\u5bb9\u91cf\u80af\u5b9a\u662f\u6709\u9650\u7684\u3002\u800c\u4e14\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b100\u4e07\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff0c\u4e0d\u4ec5\u5360\u7528\u5f88\u5927\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u5982\u679c\u6211\u4eec\u4ec5\u4ec5\u9700\u8981\u8bbf\u95ee\u524d\u9762\u51e0\u4e2a\u5143\u7d20\uff0c\u90a3\u540e\u9762\u7edd\u5927\u591a\u6570\u5143\u7d20\u5360\u7528\u7684\u7a7a\u95f4\u90fd\u767d\u767d\u6d6a\u8d39\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5982\u679c\u5217\u8868\u5143\u7d20\u53ef\u4ee5\u6309\u7167\u67d0\u79cd\u7b97\u6cd5\u63a8\u7b97\u51fa\u6765\uff0c\u90a3\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u5728\u5faa\u73af\u7684\u8fc7\u7a0b\u4e2d\u4e0d\u65ad\u63a8\u7b97\u51fa\u540e\u7eed\u7684\u5143\u7d20\u5462\uff1f\u8fd9\u6837\u5c31\u4e0d\u5fc5\u521b\u5efa\u5b8c\u6574\u7684list\uff0c\u4ece\u800c\u8282\u7701\u5927\u91cf\u7684\u7a7a\u95f4\u3002\u5728Python\u4e2d\uff0c\u8fd9\u79cd\u4e00\u8fb9\u5faa\u73af\u4e00\u8fb9\u8ba1\u7b97\u7684\u673a\u5236\uff0c\u79f0\u4e3a\u751f\u6210\u5668\uff1agenerator\u3002<\/p>\n\n\n\n<p>\u8981\u521b\u5efa\u4e00\u4e2agenerator\uff0c\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\u3002\u7b2c\u4e00\u79cd\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u53ea\u8981\u628a\u4e00\u4e2a\u5217\u8868\u751f\u6210\u5f0f\u7684<code>[]<\/code>\u6539\u6210<code>()<\/code>\uff0c\u5c31\u521b\u5efa\u4e86\u4e00\u4e2agenerator\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; L = &#91;x * x for x in range(10)]\n&gt;&gt;&gt; L\n&#91;0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n&gt;&gt;&gt; g = (x * x for x in range(10))\n&gt;&gt;&gt; g\n&lt;generator object &lt;genexpr&gt; at 0x1022ef630&gt;\n<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa<code>L<\/code>\u548c<code>g<\/code>\u7684\u533a\u522b\u4ec5\u5728\u4e8e\u6700\u5916\u5c42\u7684<code>[]<\/code>\u548c<code>()<\/code>\uff0c<code>L<\/code>\u662f\u4e00\u4e2alist\uff0c\u800c<code>g<\/code>\u662f\u4e00\u4e2agenerator\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\u51falist\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u6211\u4eec\u600e\u4e48\u6253\u5370\u51fagenerator\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u5462\uff1f<\/p>\n\n\n\n<p>\u5982\u679c\u8981\u4e00\u4e2a\u4e00\u4e2a\u6253\u5370\u51fa\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7<code>next()<\/code>\u51fd\u6570\u83b7\u5f97generator\u7684\u4e0b\u4e00\u4e2a\u8fd4\u56de\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; next(g)\n0\n&gt;&gt;&gt; next(g)\n1\n&gt;&gt;&gt; next(g)\n4\n&gt;&gt;&gt; next(g)\n9\n&gt;&gt;&gt; next(g)\n16\n&gt;&gt;&gt; next(g)\n25\n&gt;&gt;&gt; next(g)\n36\n&gt;&gt;&gt; next(g)\n49\n&gt;&gt;&gt; next(g)\n64\n&gt;&gt;&gt; next(g)\n81\n&gt;&gt;&gt; next(g)\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nStopIteration\n<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u8bb2\u8fc7\uff0cgenerator\u4fdd\u5b58\u7684\u662f\u7b97\u6cd5\uff0c\u6bcf\u6b21\u8c03\u7528<code>next(g)<\/code>\uff0c\u5c31\u8ba1\u7b97\u51fa<code>g<\/code>\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u503c\uff0c\u76f4\u5230\u8ba1\u7b97\u5230\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u6ca1\u6709\u66f4\u591a\u7684\u5143\u7d20\u65f6\uff0c\u629b\u51fa<code>StopIteration<\/code>\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4e0a\u9762\u8fd9\u79cd\u4e0d\u65ad\u8c03\u7528<code>next(g)<\/code>\u5b9e\u5728\u662f\u592a\u53d8\u6001\u4e86\uff0c\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\u56e0\u4e3agenerator\u4e5f\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; g = (x * x for x in range(10))\n&gt;&gt;&gt; for n in g:\n...     print(n)\n... \n0\n1\n4\n9\n16\n25\n36\n49\n64\n81\n<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2agenerator\u540e\uff0c\u57fa\u672c\u4e0a\u6c38\u8fdc\u4e0d\u4f1a\u8c03\u7528<code>next()<\/code>\uff0c\u800c\u662f\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u6765\u8fed\u4ee3\u5b83\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u5173\u5fc3<code>StopIteration<\/code>\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<p>generator\u975e\u5e38\u5f3a\u5927\u3002\u5982\u679c\u63a8\u7b97\u7684\u7b97\u6cd5\u6bd4\u8f83\u590d\u6742\uff0c\u7528\u7c7b\u4f3c\u5217\u8868\u751f\u6210\u5f0f\u7684<code>for<\/code>\u5faa\u73af\u65e0\u6cd5\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u8fd8\u53ef\u4ee5\u7528\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\uff0c\u8457\u540d\u7684\u6590\u6ce2\u62c9\u5951\u6570\u5217\uff08Fibonacci\uff09\uff0c\u9664\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2a\u6570\u5916\uff0c\u4efb\u610f\u4e00\u4e2a\u6570\u90fd\u53ef\u7531\u524d\u4e24\u4e2a\u6570\u76f8\u52a0\u5f97\u5230\uff1a<\/p>\n\n\n\n<p>1, 1, 2, 3, 5, 8, 13, 21, 34, &#8230;<\/p>\n\n\n\n<p>\u6590\u6ce2\u62c9\u5951\u6570\u5217\u7528\u5217\u8868\u751f\u6210\u5f0f\u5199\u4e0d\u51fa\u6765\uff0c\u4f46\u662f\uff0c\u7528\u51fd\u6570\u628a\u5b83\u6253\u5370\u51fa\u6765\u5374\u5f88\u5bb9\u6613\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fib(max):\n    n, a, b = 0, 0, 1\n    while n &lt; max:\n        print(b)\n        a, b = b, a + b\n        n = n + 1\n    return 'done'\n<\/code><\/pre>\n\n\n\n<p><em>\u6ce8\u610f<\/em>\uff0c\u8fd9\u662f\u4e00\u4e2a\u8d4b\u503c\u8bed\u53e5\uff0c\u4f60\u5e94\u8be5\u5206\u7b49\u53f7\u7684\u4e24\u8fb9\u5206\u5f00\u6765\u770b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b = b, a + b\n<\/code><\/pre>\n\n\n\n<p>\u76f8\u5f53\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>t = (b, a + b) # t\u662f\u4e00\u4e2atuple\na = t&#91;0]\nb = t&#91;1]\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u4e0d\u5fc5\u663e\u5f0f\u5199\u51fa\u4e34\u65f6\u53d8\u91cft\u5c31\u53ef\u4ee5\u8d4b\u503c\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u51fd\u6570\u53ef\u4ee5\u8f93\u51fa\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u524dN\u4e2a\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; fib(6)\n1\n1\n2\n3\n5\n8\n'done'\n<\/code><\/pre>\n\n\n\n<p>\u4ed4\u7ec6\u89c2\u5bdf\uff0c\u53ef\u4ee5\u770b\u51fa\uff0c<code>fib<\/code>\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u5b9a\u4e49\u4e86\u6590\u6ce2\u62c9\u5951\u6570\u5217\u7684\u63a8\u7b97\u89c4\u5219\uff0c\u53ef\u4ee5\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u63a8\u7b97\u51fa\u540e\u7eed\u4efb\u610f\u7684\u5143\u7d20\uff0c\u8fd9\u79cd\u903b\u8f91\u5176\u5b9e\u975e\u5e38\u7c7b\u4f3cgenerator\u3002<\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e0a\u9762\u7684\u51fd\u6570\u548cgenerator\u4ec5\u4e00\u6b65\u4e4b\u9065\u3002\u8981\u628a<code>fib<\/code>\u51fd\u6570\u53d8\u6210generator\u51fd\u6570\uff0c\u53ea\u9700\u8981\u628a<code>print(b)<\/code>\u6539\u4e3a<code>yield b<\/code>\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fib(max):\n    n, a, b = 0, 0, 1\n    while n &lt; max:\n        yield b\n        a, b = b, a + b\n        n = n + 1\n    return 'done'\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c31\u662f\u5b9a\u4e49generator\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u4e2d\u5305\u542b<code>yield<\/code>\u5173\u952e\u5b57\uff0c\u90a3\u4e48\u8fd9\u4e2a\u51fd\u6570\u5c31\u4e0d\u518d\u662f\u4e00\u4e2a\u666e\u901a\u51fd\u6570\uff0c\u800c\u662f\u4e00\u4e2agenerator\u51fd\u6570\uff0c\u8c03\u7528\u4e00\u4e2agenerator\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2agenerator\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; f = fib(6)\n&gt;&gt;&gt; f\n&lt;generator object fib at 0x104feaaa0&gt;\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c\u6700\u96be\u7406\u89e3\u7684\u5c31\u662fgenerator\u51fd\u6570\u548c\u666e\u901a\u51fd\u6570\u7684\u6267\u884c\u6d41\u7a0b\u4e0d\u4e00\u6837\u3002\u666e\u901a\u51fd\u6570\u662f\u987a\u5e8f\u6267\u884c\uff0c\u9047\u5230<code>return<\/code>\u8bed\u53e5\u6216\u8005\u6700\u540e\u4e00\u884c\u51fd\u6570\u8bed\u53e5\u5c31\u8fd4\u56de\u3002\u800c\u53d8\u6210generator\u7684\u51fd\u6570\uff0c\u5728\u6bcf\u6b21\u8c03\u7528<code>next()<\/code>\u7684\u65f6\u5019\u6267\u884c\uff0c\u9047\u5230<code>yield<\/code>\u8bed\u53e5\u8fd4\u56de\uff0c\u518d\u6b21\u6267\u884c\u65f6\u4ece\u4e0a\u6b21\u8fd4\u56de\u7684<code>yield<\/code>\u8bed\u53e5\u5904\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5b9a\u4e49\u4e00\u4e2agenerator\u51fd\u6570\uff0c\u4f9d\u6b21\u8fd4\u56de\u6570\u5b571\uff0c3\uff0c5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def odd():\n    print('step 1')\n    yield 1\n    print('step 2')\n    yield(3)\n    print('step 3')\n    yield(5)\n<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\u8be5generator\u51fd\u6570\u65f6\uff0c\u9996\u5148\u8981\u751f\u6210\u4e00\u4e2agenerator\u5bf9\u8c61\uff0c\u7136\u540e\u7528<code>next()<\/code>\u51fd\u6570\u4e0d\u65ad\u83b7\u5f97\u4e0b\u4e00\u4e2a\u8fd4\u56de\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; o = odd()\n&gt;&gt;&gt; next(o)\nstep 1\n1\n&gt;&gt;&gt; next(o)\nstep 2\n3\n&gt;&gt;&gt; next(o)\nstep 3\n5\n&gt;&gt;&gt; next(o)\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nStopIteration\n<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>odd<\/code>\u4e0d\u662f\u666e\u901a\u51fd\u6570\uff0c\u800c\u662fgenerator\u51fd\u6570\uff0c\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230<code>yield<\/code>\u5c31\u4e2d\u65ad\uff0c\u4e0b\u6b21\u53c8\u7ee7\u7eed\u6267\u884c\u3002\u6267\u884c3\u6b21<code>yield<\/code>\u540e\uff0c\u5df2\u7ecf\u6ca1\u6709<code>yield<\/code>\u53ef\u4ee5\u6267\u884c\u4e86\uff0c\u6240\u4ee5\uff0c\u7b2c4\u6b21\u8c03\u7528<code>next(o)<\/code>\u5c31\u62a5\u9519\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8bf7\u52a1\u5fc5\u6ce8\u610f\uff1a\u8c03\u7528generator\u51fd\u6570\u4f1a\u521b\u5efa\u4e00\u4e2agenerator\u5bf9\u8c61\uff0c\u591a\u6b21\u8c03\u7528generator\u51fd\u6570\u4f1a\u521b\u5efa\u591a\u4e2a\u76f8\u4e92\u72ec\u7acb\u7684generator\u3002\n<\/code><\/pre>\n\n\n\n<p>\u6709\u7684\u7ae5\u978b\u4f1a\u53d1\u73b0\u8fd9\u6837\u8c03\u7528<code>next()<\/code>\u6bcf\u6b21\u90fd\u8fd4\u56de1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; next(odd())\nstep 1\n1\n&gt;&gt;&gt; next(odd())\nstep 1\n1\n&gt;&gt;&gt; next(odd())\nstep 1\n1\n<\/code><\/pre>\n\n\n\n<p>\u539f\u56e0\u5728\u4e8e<code>odd()<\/code>\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684generator\u5bf9\u8c61\uff0c\u4e0a\u8ff0\u4ee3\u7801\u5b9e\u9645\u4e0a\u521b\u5efa\u4e863\u4e2a\u5b8c\u5168\u72ec\u7acb\u7684generator\uff0c\u5bf93\u4e2agenerator\u5206\u522b\u8c03\u7528<code>next()<\/code>\u5f53\u7136\u6bcf\u4e2a\u90fd\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u503c\u3002<\/p>\n\n\n\n<p>\u6b63\u786e\u7684\u5199\u6cd5\u662f\u521b\u5efa\u4e00\u4e2agenerator\u5bf9\u8c61\uff0c\u7136\u540e\u4e0d\u65ad\u5bf9\u8fd9\u4e00\u4e2agenerator\u5bf9\u8c61\u8c03\u7528<code>next()<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; g = odd()\n&gt;&gt;&gt; next(g)\nstep 1\n1\n&gt;&gt;&gt; next(g)\nstep 2\n3\n&gt;&gt;&gt; next(g)\nstep 3\n5\n<\/code><\/pre>\n\n\n\n<p>\u56de\u5230<code>fib<\/code>\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u5728\u5faa\u73af\u8fc7\u7a0b\u4e2d\u4e0d\u65ad\u8c03\u7528<code>yield<\/code>\uff0c\u5c31\u4f1a\u4e0d\u65ad\u4e2d\u65ad\u3002\u5f53\u7136\u8981\u7ed9\u5faa\u73af\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\u6765\u9000\u51fa\u5faa\u73af\uff0c\u4e0d\u7136\u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a\u65e0\u9650\u6570\u5217\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u540c\u6837\u7684\uff0c\u628a\u51fd\u6570\u6539\u6210generator\u51fd\u6570\u540e\uff0c\u6211\u4eec\u57fa\u672c\u4e0a\u4ece\u6765\u4e0d\u4f1a\u7528<code>next()<\/code>\u6765\u83b7\u53d6\u4e0b\u4e00\u4e2a\u8fd4\u56de\u503c\uff0c\u800c\u662f\u76f4\u63a5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6765\u8fed\u4ee3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; for n in fib(6):\n...     print(n)\n...\n1\n1\n2\n3\n5\n8<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5341\u56db<\/p>\n\n\n\n<p>\u628a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u79f0\u4e3a\u9ad8\u9636\u51fd\u6570<\/p>\n\n\n\n<p>Python\u5185\u5efa\u4e86<code>map()<\/code>\u548c<code>reduce()<\/code>\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u8bfb\u8fc7Google\u7684\u90a3\u7bc7\u5927\u540d\u9f0e\u9f0e\u7684\u8bba\u6587\u201c<a href=\"https:\/\/research.google\/pubs\/mapreduce-simplified-data-processing-on-large-clusters\/\" target=\"_blank\" rel=\"noreferrer noopener\">MapReduce: Simplified Data Processing on Large Clusters<\/a>\u201d\uff0c\u4f60\u5c31\u80fd\u5927\u6982\u660e\u767dmap\/reduce\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5148\u770bmap\u3002<code>map()<\/code>\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662f\u51fd\u6570\uff0c\u4e00\u4e2a\u662f<code>Iterable<\/code>\uff0c<code>map<\/code>\u5c06\u4f20\u5165\u7684\u51fd\u6570\u4f9d\u6b21\u4f5c\u7528\u5230\u5e8f\u5217\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u628a\u7ed3\u679c\u4f5c\u4e3a\u65b0\u7684<code>Iterator<\/code>\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u4e3e\u4f8b\u8bf4\u660e\uff0c\u6bd4\u5982\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570f(x)=x<sup>2<\/sup>\uff0c\u8981\u628a\u8fd9\u4e2a\u51fd\u6570\u4f5c\u7528\u5728\u4e00\u4e2alist&nbsp;<code>[1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\u4e0a\uff0c\u5c31\u53ef\u4ee5\u7528<code>map()<\/code>\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            f(x) = x * x\n\n                  \u2502\n                  \u2502\n  \u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2510\n  \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502\n  \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc\n\n&#91; 1   2   3   4   5   6   7   8   9 ]\n\n  \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502\n  \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502\n  \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc\n\n&#91; 1   4   9  16  25  36  49  64  81 ]\n<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u7528Python\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; def f(x):\n...     return x * x\n...\n&gt;&gt;&gt; r = map(f, &#91;1, 2, 3, 4, 5, 6, 7, 8, 9])\n&gt;&gt;&gt; list(r)\n&#91;1, 4, 9, 16, 25, 36, 49, 64, 81]\n<\/code><\/pre>\n\n\n\n<p><code>map()<\/code>\u4f20\u5165\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f<code>f<\/code>\uff0c\u5373\u51fd\u6570\u5bf9\u8c61\u672c\u8eab\u3002\u7531\u4e8e\u7ed3\u679c<code>r<\/code>\u662f\u4e00\u4e2a<code>Iterator<\/code>\uff0c<code>Iterator<\/code>\u662f\u60f0\u6027\u5e8f\u5217\uff0c\u56e0\u6b64\u901a\u8fc7<code>list()<\/code>\u51fd\u6570\u8ba9\u5b83\u628a\u6574\u4e2a\u5e8f\u5217\u90fd\u8ba1\u7b97\u51fa\u6765\u5e76\u8fd4\u56de\u4e00\u4e2alist\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u4e0d\u9700\u8981<code>map()<\/code>\u51fd\u6570\uff0c\u5199\u4e00\u4e2a\u5faa\u73af\uff0c\u4e5f\u53ef\u4ee5\u8ba1\u7b97\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>L = &#91;]\nfor n in &#91;1, 2, 3, 4, 5, 6, 7, 8, 9]:\n    L.append(f(n))\nprint(L)\n<\/code><\/pre>\n\n\n\n<p>\u7684\u786e\u53ef\u4ee5\uff0c\u4f46\u662f\uff0c\u4ece\u4e0a\u9762\u7684\u5faa\u73af\u4ee3\u7801\uff0c\u80fd\u4e00\u773c\u770b\u660e\u767d\u201c\u628af(x)\u4f5c\u7528\u5728list\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u5e76\u628a\u7ed3\u679c\u751f\u6210\u4e00\u4e2a\u65b0\u7684list\u201d\u5417\uff1f<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c<code>map()<\/code>\u4f5c\u4e3a\u9ad8\u9636\u51fd\u6570\uff0c\u4e8b\u5b9e\u4e0a\u5b83\u628a\u8fd0\u7b97\u89c4\u5219\u62bd\u8c61\u4e86\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u4f46\u53ef\u4ee5\u8ba1\u7b97\u7b80\u5355\u7684f(x)=x<sup>2<\/sup>\uff0c\u8fd8\u53ef\u4ee5\u8ba1\u7b97\u4efb\u610f\u590d\u6742\u7684\u51fd\u6570\uff0c\u6bd4\u5982\uff0c\u628a\u8fd9\u4e2alist\u6240\u6709\u6570\u5b57\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; list(map(str, &#91;1, 2, 3, 4, 5, 6, 7, 8, 9]))\n&#91;'1', '2', '3', '4', '5', '6', '7', '8', '9']\n<\/code><\/pre>\n\n\n\n<p>\u53ea\u9700\u8981\u4e00\u884c\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u518d\u770b<code>reduce<\/code>\u7684\u7528\u6cd5\u3002<code>reduce<\/code>\u628a\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u5728\u4e00\u4e2a\u5e8f\u5217<code>[x1, x2, x3, ...]<\/code>\u4e0a\uff0c\u8fd9\u4e2a\u51fd\u6570\u5fc5\u987b\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c<code>reduce<\/code>\u628a\u7ed3\u679c\u7ee7\u7eed\u548c\u5e8f\u5217\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u505a\u7d2f\u79ef\u8ba1\u7b97\uff0c\u5176\u6548\u679c\u5c31\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>reduce(f, &#91;x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)\n<\/code><\/pre>\n\n\n\n<p>\u6bd4\u65b9\u8bf4\u5bf9\u4e00\u4e2a\u5e8f\u5217\u6c42\u548c\uff0c\u5c31\u53ef\u4ee5\u7528<code>reduce<\/code>\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; from functools import reduce\n&gt;&gt;&gt; def add(x, y):\n...     return x + y\n...\n&gt;&gt;&gt; reduce(add, &#91;1, 3, 5, 7, 9])\n25\n<\/code><\/pre>\n\n\n\n<p>\u5341\u4e94   \u533f\u540d\u51fd\u6570<code>lambda<\/code><\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u5728\u4f20\u5165\u51fd\u6570\u65f6\uff0c\u6709\u4e9b\u65f6\u5019\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5b9a\u4e49\u51fd\u6570\uff0c\u76f4\u63a5\u4f20\u5165\u533f\u540d\u51fd\u6570\u66f4\u65b9\u4fbf\u3002<\/p>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u5bf9\u533f\u540d\u51fd\u6570\u63d0\u4f9b\u4e86\u6709\u9650\u652f\u6301\u3002\u8fd8\u662f\u4ee5<code>map()<\/code>\u51fd\u6570\u4e3a\u4f8b\uff0c\u8ba1\u7b97f(x)=x<sup>2<\/sup>\u65f6\uff0c\u9664\u4e86\u5b9a\u4e49\u4e00\u4e2a<code>f(x)<\/code>\u7684\u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u4f20\u5165\u533f\u540d\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; list(map(lambda x: x * x, &#91;1, 2, 3, 4, 5, 6, 7, 8, 9]))\n&#91;1, 4, 9, 16, 25, 36, 49, 64, 81]\n<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u5bf9\u6bd4\u53ef\u4ee5\u770b\u51fa\uff0c\u533f\u540d\u51fd\u6570<code>lambda x: x * x<\/code>\u5b9e\u9645\u4e0a\u5c31\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def f(x):\n    return x * x\n<\/code><\/pre>\n\n\n\n<p>\u5173\u952e\u5b57<code>lambda<\/code>\u8868\u793a\u533f\u540d\u51fd\u6570\uff0c\u5192\u53f7\u524d\u9762\u7684<code>x<\/code>\u8868\u793a\u51fd\u6570\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u533f\u540d\u51fd\u6570\u6709\u4e2a\u9650\u5236\uff0c\u5c31\u662f\u53ea\u80fd\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u4e0d\u7528\u5199<code>return<\/code>\uff0c\u8fd4\u56de\u503c\u5c31\u662f\u8be5\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u7528\u533f\u540d\u51fd\u6570\u6709\u4e2a\u597d\u5904\uff0c\u56e0\u4e3a\u51fd\u6570\u6ca1\u6709\u540d\u5b57\uff0c\u4e0d\u5fc5\u62c5\u5fc3\u51fd\u6570\u540d\u51b2\u7a81\u3002\u6b64\u5916\uff0c\u533f\u540d\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u628a\u533f\u540d\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u518d\u5229\u7528\u53d8\u91cf\u6765\u8c03\u7528\u8be5\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; f = lambda x: x * x\n&gt;&gt;&gt; f\n&lt;function &lt;lambda&gt; at 0x101c6ef28&gt;\n&gt;&gt;&gt; f(5)\n25\n<\/code><\/pre>\n\n\n\n<p>\u540c\u6837\uff0c\u4e5f\u53ef\u4ee5\u628a\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def build(x, y):\n    return lambda: x * x + y * y\n<\/code><\/pre>\n\n\n\n<p>\u5341\u516d<\/p>\n\n\n\n<p>\u6bcf\u4e00\u4e2a\u5305\u76ee\u5f55\u4e0b\u9762\u90fd\u4f1a\u6709\u4e00\u4e2a<code>__init__.py<\/code>\u7684\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u662f\u5fc5\u987b\u5b58\u5728\u7684\uff0c\u5426\u5219\uff0cPython\u5c31\u628a\u8fd9\u4e2a\u76ee\u5f55\u5f53\u6210\u666e\u901a\u76ee\u5f55\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5305\uff0c\u65e0\u6cd5\u88ab\u522b\u7684\u6587\u4ef6\u5f15\u7528\u3002<code>__init__.py<\/code>\u53ef\u4ee5\u662f\u7a7a\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u6709Python\u4ee3\u7801\uff0c\u56e0\u4e3a<code>__init__.py<\/code>\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u800c\u5b83\u7684\u6a21\u5757\u540d\u5c31\u662f<code>\u76ee\u5f55\u540d<\/code><\/p>\n\n\n\n<p>\u5341\u4e03<\/p>\n\n\n\n<p>\u5728\u7c7b\u4e2d\uff0c<code>__init__<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u6c38\u8fdc\u662f<code>self<\/code>\uff0c\u8868\u793a\u521b\u5efa\u7684\u5b9e\u4f8b\u672c\u8eab\uff0c\u56e0\u6b64\uff0c\u5728<code>__init__<\/code>\u65b9\u6cd5\u5185\u90e8\uff0c\u5c31\u53ef\u4ee5\u628a\u5404\u79cd\u5c5e\u6027\u7ed1\u5b9a\u5230<code>self<\/code>\uff0c\u56e0\u4e3a<code>self<\/code>\u5c31\u6307\u5411\u521b\u5efa\u7684\u5b9e\u4f8b\u672c\u8eab\u3002<\/p>\n\n\n\n<p>\u6709\u4e86<code>__init__<\/code>\u65b9\u6cd5\uff0c\u5728\u521b\u5efa\u5b9e\u4f8b\u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u80fd\u4f20\u5165\u7a7a\u7684\u53c2\u6570\u4e86\uff0c\u5fc5\u987b\u4f20\u5165\u4e0e<code>__init__<\/code>\u65b9\u6cd5\u5339\u914d\u7684\u53c2\u6570\uff0c\u4f46<code>self<\/code>\u4e0d\u9700\u8981\u4f20\uff0cPython\u89e3\u91ca\u5668\u81ea\u5df1\u4f1a\u628a\u5b9e\u4f8b\u53d8\u91cf\u4f20\u8fdb\u53bb\uff1a<\/p>\n\n\n\n<p>\u548c\u666e\u901a\u7684\u51fd\u6570\u76f8\u6bd4\uff0c\u5728\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u53ea\u6709\u4e00\u70b9\u4e0d\u540c\uff0c\u5c31\u662f\u7b2c\u4e00\u4e2a\u53c2\u6570\u6c38\u8fdc\u662f\u5b9e\u4f8b\u53d8\u91cf<code>self<\/code>\uff0c\u5e76\u4e14\uff0c\u8c03\u7528\u65f6\uff0c\u4e0d\u7528\u4f20\u9012\u8be5\u53c2\u6570\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u7c7b\u7684\u65b9\u6cd5\u548c\u666e\u901a\u51fd\u6570\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5341\u516b \u591a\u6001<\/p>\n\n\n\n<p>\u8981\u7406\u89e3\u4ec0\u4e48\u662f\u591a\u6001\uff0c\u6211\u4eec\u9996\u5148\u8981\u5bf9\u6570\u636e\u7c7b\u578b\u518d\u4f5c\u4e00\u70b9\u8bf4\u660e\u3002\u5f53\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2aclass\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u5c31\u5b9a\u4e49\u4e86\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u3002\u6211\u4eec\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u548cPython\u81ea\u5e26\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982str\u3001list\u3001dict\u6ca1\u4ec0\u4e48\u4e24\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = list() # a\u662flist\u7c7b\u578b\nb = Animal() # b\u662fAnimal\u7c7b\u578b\nc = Dog() # c\u662fDog\u7c7b\u578b\n<\/code><\/pre>\n\n\n\n<p>\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u67d0\u4e2a\u7c7b\u578b\u53ef\u4ee5\u7528<code>isinstance()<\/code>\u5224\u65ad\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; isinstance(a, list)\nTrue\n&gt;&gt;&gt; isinstance(b, Animal)\nTrue\n&gt;&gt;&gt; isinstance(c, Dog)\nTrue\n<\/code><\/pre>\n\n\n\n<p>\u770b\u6765<code>a<\/code>\u3001<code>b<\/code>\u3001<code>c<\/code>\u786e\u5b9e\u5bf9\u5e94\u7740<code>list<\/code>\u3001<code>Animal<\/code>\u3001<code>Dog<\/code>\u8fd93\u79cd\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u7b49\u7b49\uff0c\u8bd5\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; isinstance(c, Animal)\nTrue\n<\/code><\/pre>\n\n\n\n<p>\u770b\u6765<code>c<\/code>\u4e0d\u4ec5\u4ec5\u662f<code>Dog<\/code>\uff0c<code>c<\/code>\u8fd8\u662f<code>Animal<\/code>\uff01<\/p>\n\n\n\n<p>\u4e0d\u8fc7\u4ed4\u7ec6\u60f3\u60f3\uff0c\u8fd9\u662f\u6709\u9053\u7406\u7684\uff0c\u56e0\u4e3a<code>Dog<\/code>\u662f\u4ece<code>Animal<\/code>\u7ee7\u627f\u4e0b\u6765\u7684\uff0c\u5f53\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>Dog<\/code>\u7684\u5b9e\u4f8b<code>c<\/code>\u65f6\uff0c\u6211\u4eec\u8ba4\u4e3a<code>c<\/code>\u7684\u6570\u636e\u7c7b\u578b\u662f<code>Dog<\/code>\u6ca1\u9519\uff0c\u4f46<code>c<\/code>\u540c\u65f6\u4e5f\u662f<code>Animal<\/code>\u4e5f\u6ca1\u9519\uff0c<code>Dog<\/code>\u672c\u6765\u5c31\u662f<code>Animal<\/code>\u7684\u4e00\u79cd\uff01<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u5b9e\u4f8b\u7684\u6570\u636e\u7c7b\u578b\u662f\u67d0\u4e2a\u5b50\u7c7b\uff0c\u90a3\u5b83\u7684\u6570\u636e\u7c7b\u578b\u4e5f\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u7236\u7c7b\u3002\u4f46\u662f\uff0c\u53cd\u8fc7\u6765\u5c31\u4e0d\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; b = Animal()\n&gt;&gt;&gt; isinstance(b, Dog)\nFalse\n<\/code><\/pre>\n\n\n\n<p><code>Dog<\/code>\u53ef\u4ee5\u770b\u6210<code>Animal<\/code>\uff0c\u4f46<code>Animal<\/code>\u4e0d\u53ef\u4ee5\u770b\u6210<code>Dog<\/code>\u3002<\/p>\n\n\n\n<p>\u8981\u7406\u89e3\u591a\u6001\u7684\u597d\u5904\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u518d\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a<code>Animal<\/code>\u7c7b\u578b\u7684\u53d8\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def run_twice(animal):\n    animal.run()\n    animal.run()\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u4f20\u5165<code>Animal<\/code>\u7684\u5b9e\u4f8b\u65f6\uff0c<code>run_twice()<\/code>\u5c31\u6253\u5370\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; run_twice(Animal())\nAnimal is running...\nAnimal is running...\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u4f20\u5165<code>Dog<\/code>\u7684\u5b9e\u4f8b\u65f6\uff0c<code>run_twice()<\/code>\u5c31\u6253\u5370\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; run_twice(Dog())\nDog is running...\nDog is running...\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u4f20\u5165<code>Cat<\/code>\u7684\u5b9e\u4f8b\u65f6\uff0c<code>run_twice()<\/code>\u5c31\u6253\u5370\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; run_twice(Cat())\nCat is running...\nCat is running...\n<\/code><\/pre>\n\n\n\n<p>\u770b\u4e0a\u53bb\u6ca1\u5565\u610f\u601d\uff0c\u4f46\u662f\u4ed4\u7ec6\u60f3\u60f3\uff0c\u73b0\u5728\uff0c\u5982\u679c\u6211\u4eec\u518d\u5b9a\u4e49\u4e00\u4e2a<code>Tortoise<\/code>\u7c7b\u578b\uff0c\u4e5f\u4ece<code>Animal<\/code>\u6d3e\u751f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Tortoise(Animal):\n    def run(self):\n        print('Tortoise is running slowly...')\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u8c03\u7528<code>run_twice()<\/code>\u65f6\uff0c\u4f20\u5165<code>Tortoise<\/code>\u7684\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; run_twice(Tortoise())\nTortoise is running slowly...\nTortoise is running slowly...\n<\/code><\/pre>\n\n\n\n<p>\u4f60\u4f1a\u53d1\u73b0\uff0c\u65b0\u589e\u4e00\u4e2a<code>Animal<\/code>\u7684\u5b50\u7c7b\uff0c\u4e0d\u5fc5\u5bf9<code>run_twice()<\/code>\u505a\u4efb\u4f55\u4fee\u6539\uff0c\u5b9e\u9645\u4e0a\uff0c\u4efb\u4f55\u4f9d\u8d56<code>Animal<\/code>\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u90fd\u53ef\u4ee5\u4e0d\u52a0\u4fee\u6539\u5730\u6b63\u5e38\u8fd0\u884c\uff0c\u539f\u56e0\u5c31\u5728\u4e8e\u591a\u6001\u3002<\/p>\n\n\n\n<p>\u591a\u6001\u7684\u597d\u5904\u5c31\u662f\uff0c\u5f53\u6211\u4eec\u9700\u8981\u4f20\u5165<code>Dog<\/code>\u3001<code>Cat<\/code>\u3001<code>Tortoise<\/code>\u2026\u2026\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u63a5\u6536<code>Animal<\/code>\u7c7b\u578b\u5c31\u53ef\u4ee5\u4e86\uff0c\u56e0\u4e3a<code>Dog<\/code>\u3001<code>Cat<\/code>\u3001<code>Tortoise<\/code>\u2026\u2026\u90fd\u662f<code>Animal<\/code>\u7c7b\u578b\uff0c\u7136\u540e\uff0c\u6309\u7167<code>Animal<\/code>\u7c7b\u578b\u8fdb\u884c\u64cd\u4f5c\u5373\u53ef\u3002\u7531\u4e8e<code>Animal<\/code>\u7c7b\u578b\u6709<code>run()<\/code>\u65b9\u6cd5\uff0c\u56e0\u6b64\uff0c\u4f20\u5165\u7684\u4efb\u610f\u7c7b\u578b\uff0c\u53ea\u8981\u662f<code>Animal<\/code>\u7c7b\u6216\u8005\u5b50\u7c7b\uff0c\u5c31\u4f1a\u81ea\u52a8\u8c03\u7528\u5b9e\u9645\u7c7b\u578b\u7684<code>run()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u5c31\u662f\u591a\u6001\u7684\u610f\u601d\uff1a<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u53d8\u91cf\uff0c\u6211\u4eec\u53ea\u9700\u8981\u77e5\u9053\u5b83\u662f<code>Animal<\/code>\u7c7b\u578b\uff0c\u65e0\u9700\u786e\u5207\u5730\u77e5\u9053\u5b83\u7684\u5b50\u7c7b\u578b\uff0c\u5c31\u53ef\u4ee5\u653e\u5fc3\u5730\u8c03\u7528<code>run()<\/code>\u65b9\u6cd5\uff0c\u800c\u5177\u4f53\u8c03\u7528\u7684<code>run()<\/code>\u65b9\u6cd5\u662f\u4f5c\u7528\u5728<code>Animal<\/code>\u3001<code>Dog<\/code>\u3001<code>Cat<\/code>\u8fd8\u662f<code>Tortoise<\/code>\u5bf9\u8c61\u4e0a\uff0c\u7531\u8fd0\u884c\u65f6\u8be5\u5bf9\u8c61\u7684\u786e\u5207\u7c7b\u578b\u51b3\u5b9a\uff0c\u8fd9\u5c31\u662f\u591a\u6001\u771f\u6b63\u7684\u5a01\u529b\uff1a\u8c03\u7528\u65b9\u53ea\u7ba1\u8c03\u7528\uff0c\u4e0d\u7ba1\u7ec6\u8282\uff0c\u800c\u5f53\u6211\u4eec\u65b0\u589e\u4e00\u79cd<code>Animal<\/code>\u7684\u5b50\u7c7b\u65f6\uff0c\u53ea\u8981\u786e\u4fdd<code>run()<\/code>\u65b9\u6cd5\u7f16\u5199\u6b63\u786e\uff0c\u4e0d\u7528\u7ba1\u539f\u6765\u7684\u4ee3\u7801\u662f\u5982\u4f55\u8c03\u7528\u7684\u3002\u8fd9\u5c31\u662f\u8457\u540d\u7684\u201c\u5f00\u95ed\u201d\u539f\u5219\uff1a<\/p>\n\n\n\n<p>\u5bf9\u6269\u5c55\u5f00\u653e\uff1a\u5141\u8bb8\u65b0\u589e<code>Animal<\/code>\u5b50\u7c7b\uff1b<\/p>\n\n\n\n<p>\u5bf9\u4fee\u6539\u5c01\u95ed\uff1a\u4e0d\u9700\u8981\u4fee\u6539\u4f9d\u8d56<code>Animal<\/code>\u7c7b\u578b\u7684<code>run_twice()<\/code>\u7b49\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u7ee7\u627f\u8fd8\u53ef\u4ee5\u4e00\u7ea7\u4e00\u7ea7\u5730\u7ee7\u627f\u4e0b\u6765\uff0c\u5c31\u597d\u6bd4\u4ece\u7237\u7237\u5230\u7238\u7238\u3001\u518d\u5230\u513f\u5b50\u8fd9\u6837\u7684\u5173\u7cfb\u3002\u800c\u4efb\u4f55\u7c7b\uff0c\u6700\u7ec8\u90fd\u53ef\u4ee5\u8ffd\u6eaf\u5230\u6839\u7c7bobject\uff0c\u8fd9\u4e9b\u7ee7\u627f\u5173\u7cfb\u770b\u4e0a\u53bb\u5c31\u50cf\u4e00\u9897\u5012\u7740\u7684\u6811\u3002\u6bd4\u5982\u5982\u4e0b\u7684\u7ee7\u627f\u6811\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n                \u2502    object     \u2502\n                \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                        \u2502\n           \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n           \u2502                         \u2502\n           \u25bc                         \u25bc\n    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510           \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n    \u2502   Animal    \u2502           \u2502    Plant    \u2502\n    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518           \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n           \u2502                         \u2502\n     \u250c\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2510            \u250c\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n     \u2502            \u2502            \u2502            \u2502\n     \u25bc            \u25bc            \u25bc            \u25bc\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502   Dog   \u2502  \u2502   Cat   \u2502  \u2502  Tree   \u2502  \u2502 Flower  \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u9759\u6001\u8bed\u8a00 vs \u52a8\u6001\u8bed\u8a00<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u9759\u6001\u8bed\u8a00\uff08\u4f8b\u5982Java\uff09\u6765\u8bf4\uff0c\u5982\u679c\u9700\u8981\u4f20\u5165<code>Animal<\/code>\u7c7b\u578b\uff0c\u5219\u4f20\u5165\u7684\u5bf9\u8c61\u5fc5\u987b\u662f<code>Animal<\/code>\u7c7b\u578b\u6216\u8005\u5b83\u7684\u5b50\u7c7b\uff0c\u5426\u5219\uff0c\u5c06\u65e0\u6cd5\u8c03\u7528<code>run()<\/code>\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8ePython\u8fd9\u6837\u7684\u52a8\u6001\u8bed\u8a00\u6765\u8bf4\uff0c\u5219\u4e0d\u4e00\u5b9a\u9700\u8981\u4f20\u5165<code>Animal<\/code>\u7c7b\u578b\u3002\u6211\u4eec\u53ea\u9700\u8981\u4fdd\u8bc1\u4f20\u5165\u7684\u5bf9\u8c61\u6709\u4e00\u4e2a<code>run()<\/code>\u65b9\u6cd5\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Timer(object):\n    def run(self):\n        print('Start...')\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c31\u662f\u52a8\u6001\u8bed\u8a00\u7684\u201c\u9e2d\u5b50\u7c7b\u578b\u201d\uff0c\u5b83\u5e76\u4e0d\u8981\u6c42\u4e25\u683c\u7684\u7ee7\u627f\u4f53\u7cfb\uff0c\u4e00\u4e2a\u5bf9\u8c61\u53ea\u8981\u201c\u770b\u8d77\u6765\u50cf\u9e2d\u5b50\uff0c\u8d70\u8d77\u8def\u6765\u50cf\u9e2d\u5b50\u201d\uff0c\u90a3\u5b83\u5c31\u53ef\u4ee5\u88ab\u770b\u505a\u662f\u9e2d\u5b50\u3002<\/p>\n\n\n\n<p>Python\u7684\u201cfile-like object\u201c\u5c31\u662f\u4e00\u79cd\u9e2d\u5b50\u7c7b\u578b\u3002\u5bf9\u771f\u6b63\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c\u5b83\u6709\u4e00\u4e2a<code>read()<\/code>\u65b9\u6cd5\uff0c\u8fd4\u56de\u5176\u5185\u5bb9\u3002\u4f46\u662f\uff0c\u8bb8\u591a\u5bf9\u8c61\uff0c\u53ea\u8981\u6709<code>read()<\/code>\u65b9\u6cd5\uff0c\u90fd\u88ab\u89c6\u4e3a\u201cfile-like object\u201c\u3002\u8bb8\u591a\u51fd\u6570\u63a5\u6536\u7684\u53c2\u6570\u5c31\u662f\u201cfile-like object\u201c\uff0c\u4f60\u4e0d\u4e00\u5b9a\u8981\u4f20\u5165\u771f\u6b63\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c\u5b8c\u5168\u53ef\u4ee5\u4f20\u5165\u4efb\u4f55\u5b9e\u73b0\u4e86<code>read()<\/code>\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u7ee7\u627f\u53ef\u4ee5\u628a\u7236\u7c7b\u7684\u6240\u6709\u529f\u80fd\u90fd\u76f4\u63a5\u62ff\u8fc7\u6765\uff0c\u8fd9\u6837\u5c31\u4e0d\u5fc5\u91cd\u96f6\u505a\u8d77\uff0c\u5b50\u7c7b\u53ea\u9700\u8981\u65b0\u589e\u81ea\u5df1\u7279\u6709\u7684\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u628a\u7236\u7c7b\u4e0d\u9002\u5408\u7684\u65b9\u6cd5\u8986\u76d6\u91cd\u5199\u3002<\/p>\n\n\n\n<p>\u52a8\u6001\u8bed\u8a00\u7684\u9e2d\u5b50\u7c7b\u578b\u7279\u70b9\u51b3\u5b9a\u4e86\u7ee7\u627f\u4e0d\u50cf\u9759\u6001\u8bed\u8a00\u90a3\u6837\u662f\u5fc5\u987b\u7684<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5341\u4e5d  \u5b9e\u4f8b\u5c5e\u6027\u548c\u7c7b\u5c5e\u6027<\/p>\n\n\n\n<p>\u7531\u4e8ePython\u662f\u52a8\u6001\u8bed\u8a00\uff0c\u6839\u636e\u7c7b\u521b\u5efa\u7684\u5b9e\u4f8b\u53ef\u4ee5\u4efb\u610f\u7ed1\u5b9a\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>\u7ed9\u5b9e\u4f8b\u7ed1\u5b9a\u5c5e\u6027\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u5b9e\u4f8b\u53d8\u91cf\uff0c\u6216\u8005\u901a\u8fc7<code>self<\/code>\u53d8\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student(object):\n    def __init__(self, name):\n        self.name = name\n\ns = Student('Bob')\ns.score = 90\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\uff0c\u5982\u679c<code>Student<\/code>\u7c7b\u672c\u8eab\u9700\u8981\u7ed1\u5b9a\u4e00\u4e2a\u5c5e\u6027\u5462\uff1f\u53ef\u4ee5\u76f4\u63a5\u5728class\u4e2d\u5b9a\u4e49\u5c5e\u6027\uff0c\u8fd9\u79cd\u5c5e\u6027\u662f\u7c7b\u5c5e\u6027\uff0c\u5f52<code>Student<\/code>\u7c7b\u6240\u6709\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student(object):\n    name = 'Student'\n<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u5c5e\u6027\u540e\uff0c\u8fd9\u4e2a\u5c5e\u6027\u867d\u7136\u5f52\u7c7b\u6240\u6709\uff0c\u4f46\u7c7b\u7684\u6240\u6709\u5b9e\u4f8b\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5230\u3002\u6765\u6d4b\u8bd5\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; class Student(object):\n...     name = 'Student'\n...\n&gt;&gt;&gt; s = Student() # \u521b\u5efa\u5b9e\u4f8bs\n&gt;&gt;&gt; print(s.name) # \u6253\u5370name\u5c5e\u6027\uff0c\u56e0\u4e3a\u5b9e\u4f8b\u5e76\u6ca1\u6709name\u5c5e\u6027\uff0c\u6240\u4ee5\u4f1a\u7ee7\u7eed\u67e5\u627eclass\u7684name\u5c5e\u6027\nStudent\n&gt;&gt;&gt; print(Student.name) # \u6253\u5370\u7c7b\u7684name\u5c5e\u6027\nStudent\n&gt;&gt;&gt; s.name = 'Michael' # \u7ed9\u5b9e\u4f8b\u7ed1\u5b9aname\u5c5e\u6027\n&gt;&gt;&gt; print(s.name) # \u7531\u4e8e\u5b9e\u4f8b\u5c5e\u6027\u4f18\u5148\u7ea7\u6bd4\u7c7b\u5c5e\u6027\u9ad8\uff0c\u56e0\u6b64\uff0c\u5b83\u4f1a\u5c4f\u853d\u6389\u7c7b\u7684name\u5c5e\u6027\nMichael\n&gt;&gt;&gt; print(Student.name) # \u4f46\u662f\u7c7b\u5c5e\u6027\u5e76\u672a\u6d88\u5931\uff0c\u7528Student.name\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\nStudent\n&gt;&gt;&gt; del s.name # \u5982\u679c\u5220\u9664\u5b9e\u4f8b\u7684name\u5c5e\u6027\n&gt;&gt;&gt; print(s.name) # \u518d\u6b21\u8c03\u7528s.name\uff0c\u7531\u4e8e\u5b9e\u4f8b\u7684name\u5c5e\u6027\u6ca1\u6709\u627e\u5230\uff0c\u7c7b\u7684name\u5c5e\u6027\u5c31\u663e\u793a\u51fa\u6765\u4e86\nStudent\n<\/code><\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c\u5728\u7f16\u5199\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u5343\u4e07\u4e0d\u8981\u5bf9\u5b9e\u4f8b\u5c5e\u6027\u548c\u7c7b\u5c5e\u6027\u4f7f\u7528\u76f8\u540c\u7684\u540d\u5b57\uff0c\u56e0\u4e3a\u76f8\u540c\u540d\u79f0\u7684\u5b9e\u4f8b\u5c5e\u6027\u5c06\u5c4f\u853d\u6389\u7c7b\u5c5e\u6027\uff0c\u4f46\u662f\u5f53\u4f60\u5220\u9664\u5b9e\u4f8b\u5c5e\u6027\u540e\uff0c\u518d\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\uff0c\u8bbf\u95ee\u5230\u7684\u5c06\u662f\u7c7b\u5c5e\u6027\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4e8c\u5341     @property<\/p>\n\n\n\n<p>\u5728\u7ed1\u5b9a\u5c5e\u6027\u65f6\uff0c\u5982\u679c\u6211\u4eec\u76f4\u63a5\u628a\u5c5e\u6027\u66b4\u9732\u51fa\u53bb\uff0c\u867d\u7136\u5199\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u4f46\u662f\uff0c\u6ca1\u529e\u6cd5\u68c0\u67e5\u53c2\u6570\uff0c\u5bfc\u81f4\u53ef\u4ee5\u628a\u6210\u7ee9\u968f\u4fbf\u6539\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s = Student()\ns.score = 9999\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u663e\u7136\u4e0d\u5408\u903b\u8f91\u3002\u4e3a\u4e86\u9650\u5236<code>score<\/code>\u7684\u8303\u56f4\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a<code>set_score()<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u6210\u7ee9\uff0c\u518d\u901a\u8fc7\u4e00\u4e2a<code>get_score()<\/code>\u6765\u83b7\u53d6\u6210\u7ee9\uff0c\u8fd9\u6837\uff0c\u5728<code>set_score()<\/code>\u65b9\u6cd5\u91cc\uff0c\u5c31\u53ef\u4ee5\u68c0\u67e5\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student(object):\n    def get_score(self):\n         return self._score\n\n    def set_score(self, value):\n        if not isinstance(value, int):\n            raise ValueError('score must be an integer!')\n        if value &lt; 0 or value &gt; 100:\n            raise ValueError('score must between 0 ~ 100!')\n        self._score = value\n<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u5bf9\u4efb\u610f\u7684<code>Student<\/code>\u5b9e\u4f8b\u8fdb\u884c\u64cd\u4f5c\uff0c\u5c31\u4e0d\u80fd\u968f\u5fc3\u6240\u6b32\u5730\u8bbe\u7f6e<code>score<\/code>\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; s = Student()\n&gt;&gt;&gt; s.set_score(60) # ok!\n&gt;&gt;&gt; s.get_score()\n60\n&gt;&gt;&gt; s.set_score(9999)\nTraceback (most recent call last):\n  ...\nValueError: score must between 0 ~ 100!\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\uff0c\u4e0a\u9762\u7684\u8c03\u7528\u65b9\u6cd5\u53c8\u7565\u663e\u590d\u6742\uff0c\u6ca1\u6709\u76f4\u63a5\u7528\u5c5e\u6027\u8fd9\u4e48\u76f4\u63a5\u7b80\u5355\u3002<\/p>\n\n\n\n<p>\u6709\u6ca1\u6709\u65e2\u80fd\u68c0\u67e5\u53c2\u6570\uff0c\u53c8\u53ef\u4ee5\u7528\u7c7b\u4f3c\u5c5e\u6027\u8fd9\u6837\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u8bbf\u95ee\u7c7b\u7684\u53d8\u91cf\u5462\uff1f\u5bf9\u4e8e\u8ffd\u6c42\u5b8c\u7f8e\u7684Python\u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u8fd9\u662f\u5fc5\u987b\u8981\u505a\u5230\u7684\uff01<\/p>\n\n\n\n<p>\u8fd8\u8bb0\u5f97\u88c5\u9970\u5668\uff08decorator\uff09\u53ef\u4ee5\u7ed9\u51fd\u6570\u52a8\u6001\u52a0\u4e0a\u529f\u80fd\u5417\uff1f\u5bf9\u4e8e\u7c7b\u7684\u65b9\u6cd5\uff0c\u88c5\u9970\u5668\u4e00\u6837\u8d77\u4f5c\u7528\u3002Python\u5185\u7f6e\u7684<code>@property<\/code>\u88c5\u9970\u5668\u5c31\u662f\u8d1f\u8d23\u628a\u4e00\u4e2a\u65b9\u6cd5\u53d8\u6210\u5c5e\u6027\u8c03\u7528\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student(object):\n    @property\n    def score(self):\n        return self._score\n\n    @score.setter\n    def score(self, value):\n        if not isinstance(value, int):\n            raise ValueError('score must be an integer!')\n        if value &lt; 0 or value &gt; 100:\n            raise ValueError('score must between 0 ~ 100!')\n        self._score = value\n<\/code><\/pre>\n\n\n\n<p><code>@property<\/code>\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u6211\u4eec\u5148\u8003\u5bdf\u5982\u4f55\u4f7f\u7528\u3002\u628a\u4e00\u4e2agetter\u65b9\u6cd5\u53d8\u6210\u5c5e\u6027\uff0c\u53ea\u9700\u8981\u52a0\u4e0a<code>@property<\/code>\u5c31\u53ef\u4ee5\u4e86\uff0c\u6b64\u65f6\uff0c<code>@property<\/code>\u672c\u8eab\u53c8\u521b\u5efa\u4e86\u53e6\u4e00\u4e2a\u88c5\u9970\u5668<code>@score.setter<\/code>\uff0c\u8d1f\u8d23\u628a\u4e00\u4e2asetter\u65b9\u6cd5\u53d8\u6210\u5c5e\u6027\u8d4b\u503c\uff0c\u4e8e\u662f\uff0c\u6211\u4eec\u5c31\u62e5\u6709\u4e00\u4e2a\u53ef\u63a7\u7684\u5c5e\u6027\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; s = Student()\n&gt;&gt;&gt; s.score = 60 # OK\uff0c\u5b9e\u9645\u8f6c\u5316\u4e3as.score(60)\n&gt;&gt;&gt; s.score # OK\uff0c\u5b9e\u9645\u8f6c\u5316\u4e3as.score()\n60\n&gt;&gt;&gt; s.score = 9999\nTraceback (most recent call last):\n  ...\nValueError: score must between 0 ~ 100!\n<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\u5230\u8fd9\u4e2a\u795e\u5947\u7684<code>@property<\/code>\uff0c\u6211\u4eec\u5728\u5bf9\u5b9e\u4f8b\u5c5e\u6027\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u5c31\u77e5\u9053\u8be5\u5c5e\u6027\u5f88\u53ef\u80fd\u4e0d\u662f\u76f4\u63a5\u66b4\u9732\u7684\uff0c\u800c\u662f\u901a\u8fc7getter\u548csetter\u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<p>\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u53ea\u8bfb\u5c5e\u6027\uff0c\u53ea\u5b9a\u4e49getter\u65b9\u6cd5\uff0c\u4e0d\u5b9a\u4e49setter\u65b9\u6cd5\u5c31\u662f\u4e00\u4e2a\u53ea\u8bfb\u5c5e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student(object):\n    @property\n    def birth(self):\n        return self._birth\n\n    @birth.setter\n    def birth(self, value):\n        self._birth = value\n\n    @property\n    def age(self):\n        return 2015 - self._birth\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684<code>birth<\/code>\u662f\u53ef\u8bfb\u5199\u5c5e\u6027\uff0c\u800c<code>age<\/code>\u5c31\u662f\u4e00\u4e2a<em>\u53ea\u8bfb<\/em>\u5c5e\u6027\uff0c\u56e0\u4e3a<code>age<\/code>\u53ef\u4ee5\u6839\u636e<code>birth<\/code>\u548c\u5f53\u524d\u65f6\u95f4\u8ba1\u7b97\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u8981\u7279\u522b\u6ce8\u610f\uff1a\u5c5e\u6027\u7684\u65b9\u6cd5\u540d\u4e0d\u8981\u548c\u5b9e\u4f8b\u53d8\u91cf\u91cd\u540d\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u7684\u4ee3\u7801\u662f\u9519\u8bef\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student(object):\n    # \u65b9\u6cd5\u540d\u79f0\u548c\u5b9e\u4f8b\u53d8\u91cf\u5747\u4e3abirth:\n    @property\n    def birth(self):\n        return self.birth\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u56e0\u4e3a\u8c03\u7528<code>s.birth<\/code>\u65f6\uff0c\u9996\u5148\u8f6c\u6362\u4e3a\u65b9\u6cd5\u8c03\u7528\uff0c\u5728\u6267\u884c<code>return self.birth<\/code>\u65f6\uff0c\u53c8\u89c6\u4e3a\u8bbf\u95ee<code>self<\/code>\u7684\u5c5e\u6027\uff0c\u4e8e\u662f\u53c8\u8f6c\u6362\u4e3a\u65b9\u6cd5\u8c03\u7528<code>self.birth()<\/code>\uff0c\u9020\u6210\u65e0\u9650\u9012\u5f52\uff0c\u6700\u7ec8\u5bfc\u81f4\u6808\u6ea2\u51fa\u62a5\u9519<code>RecursionError<\/code><\/p>\n\n\n\n<p>\u4e8c\u5341\u4e00     \u591a\u8fdb\u7a0b<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u6253\u7b97\u7f16\u5199\u591a\u8fdb\u7a0b\u7684\u670d\u52a1\u7a0b\u5e8f\uff0cUnix\/Linux\u65e0\u7591\u662f\u6b63\u786e\u7684\u9009\u62e9\u3002\u7531\u4e8eWindows\u6ca1\u6709<code>fork<\/code>\u8c03\u7528\uff0c\u96be\u9053\u5728Windows\u4e0a\u65e0\u6cd5\u7528Python\u7f16\u5199\u591a\u8fdb\u7a0b\u7684\u7a0b\u5e8f\uff1f<\/p>\n\n\n\n<p>\u7531\u4e8ePython\u662f\u8de8\u5e73\u53f0\u7684\uff0c\u81ea\u7136\u4e5f\u5e94\u8be5\u63d0\u4f9b\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u591a\u8fdb\u7a0b\u652f\u6301\u3002<code>multiprocessing<\/code>\u6a21\u5757\u5c31\u662f\u8de8\u5e73\u53f0\u7248\u672c\u7684\u591a\u8fdb\u7a0b\u6a21\u5757\u3002<\/p>\n\n\n\n<p><code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Process<\/code>\u7c7b\u6765\u4ee3\u8868\u4e00\u4e2a\u8fdb\u7a0b\u5bf9\u8c61\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u542f\u52a8\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5e76\u7b49\u5f85\u5176\u7ed3\u675f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from multiprocessing import Process\nimport os\n\n# \u5b50\u8fdb\u7a0b\u8981\u6267\u884c\u7684\u4ee3\u7801\ndef run_proc(name):\n    print('Run child process %s (%s)...' % (name, os.getpid()))\n\nif __name__=='__main__':\n    print('Parent process %s.' % os.getpid())\n    p = Process(target=run_proc, args=('test',))\n    print('Child process will start.')\n    p.start()\n    p.join()\n    print('Child process end.')\n<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Parent process 928.\nChild process will start.\nRun child process test (929)...\nProcess end.\n<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u5b50\u8fdb\u7a0b\u65f6\uff0c\u53ea\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u6267\u884c\u51fd\u6570\u548c\u51fd\u6570\u7684\u53c2\u6570\uff0c\u521b\u5efa\u4e00\u4e2a<code>Process<\/code>\u5b9e\u4f8b\uff0c\u7528<code>start()<\/code>\u65b9\u6cd5\u542f\u52a8\uff0c\u8fd9\u6837\u521b\u5efa\u8fdb\u7a0b\u6bd4<code>fork()<\/code>\u8fd8\u8981\u7b80\u5355\u3002<\/p>\n\n\n\n<p><code>join()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\u540e\u518d\u7ee7\u7eed\u5f80\u4e0b\u8fd0\u884c\uff0c\u901a\u5e38\u7528\u4e8e\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pool<\/h3>\n\n\n\n<p>\u5982\u679c\u8981\u542f\u52a8\u5927\u91cf\u7684\u5b50\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u7528\u8fdb\u7a0b\u6c60\u7684\u65b9\u5f0f\u6279\u91cf\u521b\u5efa\u5b50\u8fdb\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from multiprocessing import Pool\nimport os, time, random\n\ndef long_time_task(name):\n    print('Run task %s (%s)...' % (name, os.getpid()))\n    start = time.time()\n    time.sleep(random.random() * 3)\n    end = time.time()\n    print('Task %s runs %0.2f seconds.' % (name, (end - start)))\n\nif __name__=='__main__':\n    print('Parent process %s.' % os.getpid())\n    p = Pool(4)\n    for i in range(5):\n        p.apply_async(long_time_task, args=(i,))\n    print('Waiting for all subprocesses done...')\n    p.close()\n    p.join()\n    print('All subprocesses done.')\n<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Parent process 669.\nWaiting for all subprocesses done...\nRun task 0 (671)...\nRun task 1 (672)...\nRun task 2 (673)...\nRun task 3 (674)...\nTask 2 runs 0.14 seconds.\nRun task 4 (673)...\nTask 1 runs 0.27 seconds.\nTask 3 runs 0.86 seconds.\nTask 0 runs 1.41 seconds.\nTask 4 runs 1.91 seconds.\nAll subprocesses done.\n<\/code><\/pre>\n\n\n\n<p>\u4ee3\u7801\u89e3\u8bfb\uff1a<\/p>\n\n\n\n<p>\u5bf9<code>Pool<\/code>\u5bf9\u8c61\u8c03\u7528<code>join()<\/code>\u65b9\u6cd5\u4f1a\u7b49\u5f85\u6240\u6709\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\uff0c\u8c03\u7528<code>join()<\/code>\u4e4b\u524d\u5fc5\u987b\u5148\u8c03\u7528<code>close()<\/code>\uff0c\u8c03\u7528<code>close()<\/code>\u4e4b\u540e\u5c31\u4e0d\u80fd\u7ee7\u7eed\u6dfb\u52a0\u65b0\u7684<code>Process<\/code>\u4e86\u3002<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\u8f93\u51fa\u7684\u7ed3\u679c\uff0ctask&nbsp;<code>0<\/code>\uff0c<code>1<\/code>\uff0c<code>2<\/code>\uff0c<code>3<\/code>\u662f\u7acb\u523b\u6267\u884c\u7684\uff0c\u800ctask&nbsp;<code>4<\/code>\u8981\u7b49\u5f85\u524d\u9762\u67d0\u4e2atask\u5b8c\u6210\u540e\u624d\u6267\u884c\uff0c\u8fd9\u662f\u56e0\u4e3a<code>Pool<\/code>\u7684\u9ed8\u8ba4\u5927\u5c0f\u5728\u6211\u7684\u7535\u8111\u4e0a\u662f4\uff0c\u56e0\u6b64\uff0c\u6700\u591a\u540c\u65f6\u6267\u884c4\u4e2a\u8fdb\u7a0b\u3002\u8fd9\u662f<code>Pool<\/code>\u6709\u610f\u8bbe\u8ba1\u7684\u9650\u5236\uff0c\u5e76\u4e0d\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u9650\u5236\u3002\u5982\u679c\u6539\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p = Pool(5)\n<\/code><\/pre>\n\n\n\n<p>\u5c31\u53ef\u4ee5\u540c\u65f6\u8dd15\u4e2a\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e<code>Pool<\/code>\u7684\u9ed8\u8ba4\u5927\u5c0f\u662fCPU\u7684\u6838\u6570\uff0c\u5982\u679c\u4f60\u4e0d\u5e78\u62e5\u67098\u6838CPU\uff0c\u4f60\u8981\u63d0\u4ea4\u81f3\u5c119\u4e2a\u5b50\u8fdb\u7a0b\u624d\u80fd\u770b\u5230\u4e0a\u9762\u7684\u7b49\u5f85\u6548\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b50\u8fdb\u7a0b<\/h3>\n\n\n\n<p>\u5f88\u591a\u65f6\u5019\uff0c\u5b50\u8fdb\u7a0b\u5e76\u4e0d\u662f\u81ea\u8eab\uff0c\u800c\u662f\u4e00\u4e2a\u5916\u90e8\u8fdb\u7a0b\u3002\u6211\u4eec\u521b\u5efa\u4e86\u5b50\u8fdb\u7a0b\u540e\uff0c\u8fd8\u9700\u8981\u63a7\u5236\u5b50\u8fdb\u7a0b\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002<\/p>\n\n\n\n<p><code>subprocess<\/code>\u6a21\u5757\u53ef\u4ee5\u8ba9\u6211\u4eec\u975e\u5e38\u65b9\u4fbf\u5730\u542f\u52a8\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u63a7\u5236\u5176\u8f93\u5165\u548c\u8f93\u51fa\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u5728Python\u4ee3\u7801\u4e2d\u8fd0\u884c\u547d\u4ee4<code>nslookup www.python.org<\/code>\uff0c\u8fd9\u548c\u547d\u4ee4\u884c\u76f4\u63a5\u8fd0\u884c\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess\n\nprint('$ nslookup www.python.org')\nr = subprocess.call(&#91;'nslookup', 'www.python.org'])\nprint('Exit code:', r)\n<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ nslookup www.python.org\nServer:\t\t192.168.19.4\nAddress:\t192.168.19.4#53\n\nNon-authoritative answer:\nwww.python.org\tcanonical name = python.map.fastly.net.\nName:\tpython.map.fastly.net\nAddress: 199.27.79.223\n\nExit code: 0\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5b50\u8fdb\u7a0b\u8fd8\u9700\u8981\u8f93\u5165\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7<code>communicate()<\/code>\u65b9\u6cd5\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess\n\nprint('$ nslookup')\np = subprocess.Popen(&#91;'nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\noutput, err = p.communicate(b'set q=mx\\npython.org\\nexit\\n')\nprint(output.decode('utf-8'))\nprint('Exit code:', p.returncode)\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u76f8\u5f53\u4e8e\u5728\u547d\u4ee4\u884c\u6267\u884c\u547d\u4ee4<code>nslookup<\/code>\uff0c\u7136\u540e\u624b\u52a8\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set q=mx\npython.org\nexit\n<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ nslookup\nServer:\t\t192.168.19.4\nAddress:\t192.168.19.4#53\n\nNon-authoritative answer:\npython.org\tmail exchanger = 50 mail.python.org.\n\nAuthoritative answers can be found from:\nmail.python.org\tinternet address = 82.94.164.166\nmail.python.org\thas AAAA address 2001:888:2000:d::a6\n\n\nExit code: 0\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h3>\n\n\n\n<p><code>Process<\/code>\u4e4b\u95f4\u80af\u5b9a\u662f\u9700\u8981\u901a\u4fe1\u7684\uff0c\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u4e86\u5f88\u591a\u673a\u5236\u6765\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u5305\u88c5\u4e86\u5e95\u5c42\u7684\u673a\u5236\uff0c\u63d0\u4f9b\u4e86<code>Queue<\/code>\u3001<code>Pipes<\/code>\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u4ea4\u6362\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4ee5<code>Queue<\/code>\u4e3a\u4f8b\uff0c\u5728\u7236\u8fdb\u7a0b\u4e2d\u521b\u5efa\u4e24\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u4e00\u4e2a\u5f80<code>Queue<\/code>\u91cc\u5199\u6570\u636e\uff0c\u4e00\u4e2a\u4ece<code>Queue<\/code>\u91cc\u8bfb\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from multiprocessing import Process, Queue\nimport os, time, random\n\n# \u5199\u6570\u636e\u8fdb\u7a0b\u6267\u884c\u7684\u4ee3\u7801:\ndef write(q):\n    print('Process to write: %s' % os.getpid())\n    for value in &#91;'A', 'B', 'C']:\n        print('Put %s to queue...' % value)\n        q.put(value)\n        time.sleep(random.random())\n\n# \u8bfb\u6570\u636e\u8fdb\u7a0b\u6267\u884c\u7684\u4ee3\u7801:\ndef read(q):\n    print('Process to read: %s' % os.getpid())\n    while True:\n        value = q.get(True)\n        print('Get %s from queue.' % value)\n\nif __name__=='__main__':\n    # \u7236\u8fdb\u7a0b\u521b\u5efaQueue\uff0c\u5e76\u4f20\u7ed9\u5404\u4e2a\u5b50\u8fdb\u7a0b\uff1a\n    q = Queue()\n    pw = Process(target=write, args=(q,))\n    pr = Process(target=read, args=(q,))\n    # \u542f\u52a8\u5b50\u8fdb\u7a0bpw\uff0c\u5199\u5165:\n    pw.start()\n    # \u542f\u52a8\u5b50\u8fdb\u7a0bpr\uff0c\u8bfb\u53d6:\n    pr.start()\n    # \u7b49\u5f85pw\u7ed3\u675f:\n    pw.join()\n    # pr\u8fdb\u7a0b\u91cc\u662f\u6b7b\u5faa\u73af\uff0c\u65e0\u6cd5\u7b49\u5f85\u5176\u7ed3\u675f\uff0c\u53ea\u80fd\u5f3a\u884c\u7ec8\u6b62:\n    pr.terminate()\n<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Process to write: 50563\nPut A to queue...\nProcess to read: 50564\nGet A from queue.\nPut B to queue...\nGet B from queue.\nPut C to queue...\nGet C from queue.<\/code><\/pre>\n\n\n\n<p>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u6700\u5927\u7684\u4e0d\u540c\u5728\u4e8e\uff0c\u591a\u8fdb\u7a0b\u4e2d\uff0c\u540c\u4e00\u4e2a\u53d8\u91cf\uff0c\u5404\u81ea\u6709\u4e00\u4efd\u62f7\u8d1d\u5b58\u5728\u4e8e\u6bcf\u4e2a\u8fdb\u7a0b\u4e2d\uff0c\u4e92\u4e0d\u5f71\u54cd\uff0c\u800c\u591a\u7ebf\u7a0b\u4e2d\uff0c\u6240\u6709\u53d8\u91cf\u90fd\u7531\u6240\u6709\u7ebf\u7a0b\u5171\u4eab\uff0c\u6240\u4ee5\uff0c\u4efb\u4f55\u4e00\u4e2a\u53d8\u91cf\u90fd\u53ef\u4ee5\u88ab\u4efb\u4f55\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539<\/p>\n\n\n\n<p>Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u4e0d\u4f46\u652f\u6301\u591a\u8fdb\u7a0b\uff0c\u5176\u4e2d<code>managers<\/code>\u5b50\u6a21\u5757\u8fd8\u652f\u6301\u628a\u591a\u8fdb\u7a0b\u5206\u5e03\u5230\u591a\u53f0\u673a\u5668\u4e0a\u3002\u4e00\u4e2a\u670d\u52a1\u8fdb\u7a0b\u53ef\u4ee5\u4f5c\u4e3a\u8c03\u5ea6\u8005\uff0c\u5c06\u4efb\u52a1\u5206\u5e03\u5230\u5176\u4ed6\u591a\u4e2a\u8fdb\u7a0b\u4e2d\uff0c\u4f9d\u9760\u7f51\u7edc\u901a\u4fe1\u3002\u7531\u4e8e<code>managers<\/code>\u6a21\u5757\u5c01\u88c5\u5f88\u597d\uff0c\u4e0d\u5fc5\u4e86\u89e3\u7f51\u7edc\u901a\u4fe1\u7684\u7ec6\u8282\uff0c\u5c31\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u7f16\u5199\u5206\u5e03\u5f0f\u591a\u8fdb\u7a0b\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u81ea\u5b66\u4e60\u65f6https:\/\/liaoxuefeng.com\/books\/python\/introduction\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-744","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=744"}],"version-history":[{"count":21,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":2165,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/744\/revisions\/2165"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}