{"id":130,"date":"2026-08-05T08:25:48","date_gmt":"2026-08-05T00:25:48","guid":{"rendered":"https:\/\/www.cloudnote.cc\/?p=130"},"modified":"2026-08-05T08:25:48","modified_gmt":"2026-08-05T00:25:48","slug":"%e3%80%90python%e3%80%91%e9%97%ad%e5%8c%85%e7%8e%b0%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/www.cloudnote.cc\/index.php\/2026\/08\/05\/%e3%80%90python%e3%80%91%e9%97%ad%e5%8c%85%e7%8e%b0%e8%b1%a1\/","title":{"rendered":"\u3010Python\u3011\u95ed\u5305\u73b0\u8c61"},"content":{"rendered":"<p>\u95ed\u5305\u5c31\u662f\u80fd\u591f\u8bfb\u53d6\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u51fd\u6570\n\u5f53\u4e00\u4e2a\u5d4c\u5957\u7684\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u4e14\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u4e86\u8fd9\u4e2a\u5185\u90e8\u51fd\u6570\u65f6\uff0c\u5c31\u5f62\u6210\u4e86\u95ed\u5305\u3002\u6b64\u65f6\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c\u88ab\u5f15\u7528\u7684\u53d8\u91cf\u4e5f\u4f1a\u88ab\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u4e0d\u4f1a\u88ab\u5783\u573e\u56de\u6536\u3002<\/p>\n<h5>\u5f62\u6210\u95ed\u5305\u7684\u4e09\u4e2a\u5fc5\u8981\u6761\u4ef6<\/h5>\n<ul>\n<li><strong><em>\u5fc5\u987b\u6709\u5d4c\u5957\u51fd\u6570<\/em><\/strong>\uff1a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u4e86\u53e6\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u3002<\/li>\n<li><strong><em>\u5185\u90e8\u51fd\u6570\u4f7f\u7528\u4e86\u5916\u90e8\u53d8\u91cf<\/em><\/strong>\uff1a\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff08\u975e\u5168\u5c40\u53d8\u91cf\uff09\u3002 <\/li>\n<li><strong><em>\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u4e86\u5185\u90e8\u51fd\u6570<\/em><\/strong>\uff1a\u5916\u5c42\u51fd\u6570\u5c06\u8fd9\u4e2a\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u3002 <\/li>\n<\/ul>\n<p>\u6211\u4eec\u77e5\u9053,\u51fd\u6570\u5185\u58f0\u660e\u7684\u53d8\u91cf\u5728\u51fd\u6570\u6267\u884c\u5b8c\u540e\u5c31\u4f1a\u9500\u6bc1,\u800cglobal\u5f80\u5f80\u662f\u5f15\u7528\u4e86\u5916\u90e8\u7684\u5168\u5c40\u53d8\u91cf,\u4e0d\u5728\u6211\u4eec\u8bf4\u7684\u51fd\u6570\u5185\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u58f0\u660e\u8303\u56f4,\u793a\u4f8b1<\/p>\n<pre><code class=\"lang-python language-python python\">num = 1\n\nnum_id = id(num)\nprint(&quot;\u5168\u5c40num\u7684id:&quot;, num_id)\n\ndef f1():\n    global num\n    num1 = 100\n    print(&quot;\u901a\u8fc7global\u58f0\u660e\u7684num\u662f\u5168\u5c40\u7684\u90a3\u4e2anum\u5417?&quot;, id(num) == num_id)\n    print(&quot;\u51fd\u6570\u5185\u90e8\u7684num1:&quot;, num1, &quot;id\u4e3a:&quot;, id(num1))\n\nprint(&quot;------\u8c03\u7528\u51fd\u6570f1------&quot;)\nf1()\n\nprint(&quot;------f1\u8c03\u7528\u540e------&quot;)\nnum2 = 100\nprint(&quot;num=&quot;, num, &quot;id\u4e3a&quot;, id(num))\nprint(&quot;num=2&quot;, num, &quot;id\u4e3a&quot;, id(num2))\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>\u5168\u5c40num\u7684id: 4350651320\n------\u8c03\u7528\u51fd\u6570f1------\n\u901a\u8fc7global\u58f0\u660e\u7684num\u662f\u5168\u5c40\u7684\u90a3\u4e2anum\u5417? True\n\u51fd\u6570\u5185\u90e8\u7684num1: 100 id\u4e3a: 4350654488\n------f1\u8c03\u7528\u540e------\nnum= 1 id\u4e3a 4350651320\nnum=2 1 id\u4e3a 4350654488<\/code><\/pre>\n<p>\u7ed3\u679c\u663e\u793a\u5168\u5c40num\u53d8\u91cf\u59cb\u7ec8\u540c\u4e00\u4e2a,\u4f46\u662fnum1\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u4e3a100,\u5730\u5740\u4e3a4350654488,\u6211\u4eec\u5728\u51fd\u6570\u5916\u90e8\u5c06100\u8d4b\u503c\u7ed9num2,\u7ed3\u679c\u5730\u5740\u8ddf\u51fd\u6570\u5185\u90e8\u58f0\u660e\u7684100\u662f\u4e00\u6837\u7684\u3002\n\u5176\u5b9e\u8fd9\u5c31\u662f\u89e6\u53d1\u4e86Python \u5185\u90e8\u5b58\u5728\u5c0f\u6574\u6570\u5bf9\u8c61\u6c60\uff08-5 \u5230 256\uff09\u548c\u5b57\u7b26\u4e32\u9a7b\u7559\u673a\u5236<\/p>\n<p>\u8fd9\u91cc\u6709\u4e2a\u731c\u60f3,num1\u662f\u5c5e\u4e8e\u4e0d\u53ef\u53d8\u91cf,\u6211\u4eec\u77e5\u9053\u5168\u5c40\u58f0\u660e\u7684\u4e0d\u53ef\u53d8\u91cf\u4f1a\u5728\u5185\u5b58\u9a7b\u7559,\u90a3\u51fd\u6570\u5185\u90e8\u58f0\u660e\u7684\u4e0d\u53ef\u53d8\u91cf\u662f\u5426\u4e5f\u4f1a\u9a7b\u7559\u5462?\n\u793a\u4f8b2:<\/p>\n<pre><code class=\"lang-python language-python python\">def f():\n    s1 = &quot;hello&quot;\n    b1 = True\n    t1 = (&quot;hello&quot;, &quot;world&quot;)\n    f1 = 100.123\n    print(f&quot;\u51fd\u6570\u5185\u90e8s1\u7684id:{id(s1)},\u51fd\u6570\u5185\u90e8b1\u7684id:{id(b1)},\u51fd\u6570\u5185\u90e8t1\u7684id:{id(t1)},\u51fd\u6570\u5185\u90e8f1\u7684id:{id(f1)}&quot;)\n\nf()\ns2 = &quot;hello&quot;\nb2 = True\nt2 = (&quot;hello&quot;, &quot;world&quot;)\nf2 = 100.123\nprint(f&quot;\u51fd\u6570\u5916\u90e8s2\u7684id:{id(s2)},\u51fd\u6570\u5916\u90e8b2\u7684id:{id(b2)},\u51fd\u6570\u5916\u90e8t2\u7684id:{id(t2)},\u51fd\u6570\u5916\u90e8f2\u7684id:{id(f2)}&quot;)<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>\u51fd\u6570\u5185\u90e8s1\u7684id:4386080640,\u51fd\u6570\u5185\u90e8b1\u7684id:4383465672,\u51fd\u6570\u5185\u90e8t1\u7684id:4386155584,\u51fd\u6570\u5185\u90e8f1\u7684id:4385172688\n\u51fd\u6570\u5916\u90e8s2\u7684id:4386080640,\u51fd\u6570\u5916\u90e8b2\u7684id:4383465672,\u51fd\u6570\u5916\u90e8t2\u7684id:4386155584,\u51fd\u6570\u5916\u90e8f2\u7684id:4385172688<\/code><\/pre>\n<p>\u8868\u9762\u4e0a\u770b,\u6211\u4eec\u8c03\u7528\u4e86f()\u5148\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u4e86\u4e0d\u53ef\u53d8\u91cf\u7c7b\u578b,\u6839\u636e\u4e0d\u53ef\u53d8\u91cf\u521b\u5efa\u65f6\u4f9d\u636e\u5bf9\u8c61\u6c60\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u539f\u5219,s2,b2,t2,f2\u8d4b\u503c\u65f6\u76f4\u63a5\u62ff\u5730\u5740,\u6240\u6709id\u90fd\u4e00\u6837,\u4e00\u5207\u89e3\u91ca\u7684\u901a,\u4f46\u662f\u5176\u5b9e\u5e76\u4e0d\u662f\u8fd9\u6837,\u518d\u770b\u4f8b\u5b50:<\/p>\n<pre><code class=\"lang-python language-python python\">def f():\n    s1 = &quot;hello&quot;\n    b1 = True\n    t1 = (&quot;hello&quot;, &quot;world&quot;)\n    f1 = 100.123\n    print(f&quot;\u51fd\u6570\u5185\u90e8s1\u7684id:{id(s1)},\u51fd\u6570\u5185\u90e8b1\u7684id:{id(b1)},\u51fd\u6570\u5185\u90e8t1\u7684id:{id(t1)},\u51fd\u6570\u5185\u90e8f1\u7684id:{id(f1)}&quot;)\n\nprint(f.__code__.co_consts)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>(None, 'hello', True, ('hello', 'world'), 100.123, '\u51fd\u6570\u5185\u90e8s1\u7684id:', ',\u51fd\u6570\u5185\u90e8b1\u7684id:', ',\u51fd\u6570\u5185\u90e8t1\u7684id:', ',\u51fd\u6570\u5185\u90e8f1\u7684id:')<\/code><\/pre>\n<p>\u6211\u4eec\u6839\u672c\u5c31\u6ca1\u6709\u8c03\u7528f\u51fd\u6570,\u4f46\u662f\u91cc\u9762\u5374\u5df2\u7ecf\u6709\u4e86\u76f8\u5173\u503c,\u8fd9\u4e2a\u5176\u5b9e\u5728\u9884\u7f16\u8bd1\u9636\u6bb5\u5c31\u53d1\u751f\u4e86\u3002\n<strong><em>\u9884\u7f16\u8bd1\u4e0e\u5e38\u91cf\u6298\u53e0<\/em><\/strong>\nPython \u5728\u6b63\u5f0f\u8fd0\u884c\u7b2c\u4e00\u884c\u4ee3\u7801\u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u201c\u6574\u4f53\u901a\u8bfb\u5e76\u7f16\u8bd1\u201d\u7684\u9636\u6bb5\uff1a<\/p>\n<ul>\n<li>\u5f53 Python \u89e3\u91ca\u5668\u8bfb\u5165\u8fd9\u4e2a .py \u6587\u4ef6\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e00\u6b21\u6027\u628a\u6574\u4e2a\u6587\u4ef6\u7ffb\u8bd1\u6210\u5b57\u8282\u7801\u3002\u5728\u7f16\u8bd1\u51fd\u6570 f \u65f6\uff0c\u5b83\u654f\u9510\u5730\u6355\u6349\u5230\u4e86\u4ee3\u7801\u91cc\u5199\u6b7b\u7684\u8fd9\u4e9b\u4e0d\u53ef\u53d8\u5b57\u9762\u91cf\u3002<\/li>\n<li>\u63d0\u524d\u5206\u914d\u5185\u5b58\uff1a\u7f16\u8bd1\u5668\u63d0\u524d\u5728\u5185\u5b58\u4e2d\u5f00\u8f9f\u4e86\u4e00\u5757\u7a7a\u95f4\uff0c\u628a None\u3001&#8221;hello&#8221;\u3001True\u3001(&#8220;hello&#8221;, &#8220;world&#8221;) \u548c 100.123 \u7edf\u4e00\u6253\u5305\uff0c\u6302\u8f7d\u5230\u4e86\u51fd\u6570\u5bf9\u8c61\u7684 co_consts\uff08\u5e38\u91cf\u8868\uff09\u5c5e\u6027\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong><em>\u6838\u5fc3\u7ed3\u8bba<\/em><\/strong>:<\/p>\n<ul>\n<li>\u975e\u7eaf\u7cb9\u9010\u884c\u89e3\u6790\uff1aPython \u7edd\u4e0d\u662f\u201c\u8bfb\u4e00\u884c\u7ffb\u8bd1\u4e00\u884c\u201d\u3002\u5b83\u7684\u6267\u884c\u6a21\u5f0f\u662f\u201c\u5148\u6574\u4f53\u9884\u7f16\u8bd1\uff0c\u518d\u52a8\u6001\u9010\u884c\u89e3\u91ca\u6267\u884c\u201d\u3002<\/li>\n<li>\u5e38\u9a7b\u539f\u56e0\u975e GC \u5931\u6548\uff1a\u540e\u7eed\u54ea\u6015\u4f60\u8c03\u7528\u5e76\u7ed3\u675f\u4e86 f()\uff0c\u51fd\u6570\u5185\u7684\u8fd9\u4e9b\u4e0d\u53ef\u53d8\u91cf\u4e5f\u4e0d\u4f1a\u88ab\u5783\u573e\u56de\u6536\uff08GC\uff09\u3002\u539f\u56e0\u4e0d\u662f\u5b83\u4eec\u6709\u4ec0\u4e48\u5e38\u9a7b\u7279\u6743\uff0c\u800c\u662f\u56e0\u4e3a\u51fd\u6570\u7684\u5e38\u91cf\u8868\uff08co_consts\uff09\u4f5c\u4e3a\u5168\u5c40\u9759\u6001\u8d44\u6e90\uff0c\u4e00\u76f4\u7262\u7262\u5730\u5f15\u7528\u7740\u5b83\u4eec\uff0c\u5bfc\u81f4\u5b83\u4eec\u7684\u5f15\u7528\u8ba1\u6570\u6c38\u8fdc\u4e0d\u4f1a\u5f52\u96f6\u3002<\/li>\n<li>\u4f5c\u7528\u57df\u5730\u5740\u5408\u5e76\uff1a\u6b63\u56e0\u5982\u6b64\uff0c\u5f53\u4f60\u5728\u51fd\u6570\u5916\u90e8\u5199\u4e0b\u4e00\u6a21\u4e00\u6837\u7684\u5b57\u9762\u91cf\uff08\u5982 f2 = 100.123\uff09\u65f6\uff0cPython \u7f16\u8bd1\u5668\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u7684\u5e38\u91cf\u6c60\u91cc\u76f4\u63a5\u627e\u5230\u4e86\u73b0\u6210\u7684\u5bf9\u8c61\uff0c\u8fd9\u624d\u4fc3\u6210\u4e86\u6211\u4eec\u5728\u524d\u51e0\u8f6e\u6d4b\u8bd5\u4e2d\u770b\u5230\u7684\u201c\u51fd\u6570\u5185\u5916 id \u5b8c\u5168\u4e00\u81f4\u201d\u7684\u5185\u5b58\u795e\u8bdd\u3002<\/li>\n<\/ul>\n<p>\u662f\u4e0d\u662f\u53ea\u8981\u662f\u4e0d\u53ef\u53d8\u91cf\u5c31\u4f1a\u6709\u9884\u7f16\u8bd1\u5462?\n\u793a\u4f8b:<\/p>\n<pre><code>def f():\n    s1 = &quot;hello&quot;\n    b1 = True\n    t1 = (&quot;hello&quot;, &quot;world&quot;)\n    f1 = float(&quot;100.123&quot;)\n    print(f&quot;\u51fd\u6570\u5185\u90e8s1\u7684id:{id(s1)},\u51fd\u6570\u5185\u90e8b1\u7684id:{id(b1)},\u51fd\u6570\u5185\u90e8t1\u7684id:{id(t1)},\u51fd\u6570\u5185\u90e8f1\u7684id:{id(f1)}&quot;)\n\nprint(f.__code__.co_consts)\n<\/code><\/pre>\n<pre><code>\u8fd0\u884c\u7ed3\u679c\n(None, 'hello', True, ('hello', 'world'), '100.123', '\u51fd\u6570\u5185\u90e8s1\u7684id:', ',\u51fd\u6570\u5185\u90e8b1\u7684id:', ',\u51fd\u6570\u5185\u90e8t1\u7684id:', ',\u51fd\u6570\u5185\u90e8f1\u7684id:')<\/code><\/pre>\n<p>\u6211\u4eec\u5728\u51fd\u6570\u5185\u90e8\u901a\u8fc7float\u5c06\u5b57\u7b26\u4e32&#8221;100.123&#8243;\u8f6c\u5316\u4e3afloat\u7c7b\u578b,\u4f46\u662fco_consts\u6253\u5370\u7684\u662f\u5b57\u7b26\u4e32,\u610f\u5473\u7740\u8fd0\u884c\u5230\u8fd9\u4e00\u884c\u65f6\uff0cPython \u5fc5\u987b\u52a8\u6001\u8c03\u7528 float() \u5e76\u5728\u5806\u5185\u5b58\u4e2d\u4e34\u65f6\u5f00\u8f9f\u4e00\u5757\u7a7a\u95f4\u6765\u5b58\u653e\u8fd9\u4e2a\u6d6e\u70b9\u6570\u3002<\/p>\n<p>\u6240\u4ee5\u9884\u7f16\u8bd1\u5176\u5b9e\u662f\u8fd9\u6837\u7684:\n<strong><em>\u9884\u7f16\u8bd1\u662f Python \u7f16\u8bd1\u5668\u9488\u5bf9\u201c\u5df2\u77e5\u3001\u786e\u5b9a\u3001\u4e14\u4e0d\u53ef\u53d8\u201d\u7684\u5b57\u9762\u91cf\u6240\u505a\u7684\u4e00\u79cd\u7a7a\u95f4\u6362\u65f6\u95f4\u7684\u4f18\u5316\u624b\u6bb5\u3002\u53ea\u8981\u4f60\u7684\u4e0d\u53ef\u53d8\u53d8\u91cf\u6cbe\u67d3\u4e86\u201c\u51fd\u6570\u8c03\u7528\u201d\u3001\u201c\u52a8\u6001\u62fc\u63a5\u201d\u3001\u201c\u8fd0\u884c\u65f6\u8ba1\u7b97\u201d\u6216\u8005\u201c\u5185\u90e8\u5305\u542b\u53ef\u53d8\u5bf9\u8c61\u201d\uff0c\u5b83\u5c31\u65e0\u6cd5\u4eab\u53d7\u9884\u7f16\u8bd1\u7684\u7ea2\u5229\uff0c\u53ea\u80fd\u4e56\u4e56\u5728\u8fd0\u884c\u671f\u52a8\u6001\u521b\u5efa\uff0c\u5e76\u5728\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6\u88ab\u5783\u573e\u56de\u6536\uff08GC\uff09\u9500\u6bc1\u3002<\/em><\/strong><\/p>\n<p>\u90a3\u53ef\u53d8\u5bf9\u8c61\u662f\u5426\u53ef\u9884\u7f16\u8bd1\u8fdb\u5e38\u91cf\u8868\uff1f\n\u90fd\u8bf4\u662f\u5e38\u4eae\u91cf\u8868\u4e86,\u53ef\u53d8\u600e\u4e48\u53ef\u4ee5\u7f16\u8bd1\u8fdb\u53bb\u5462\n\u72ec\u7acb\u5185\u5b58\u4e0e\u9632\u6c61\u67d3\u673a\u5236\n\u5217\u8868\u662f\u53ef\u53d8\u7684\uff08Mutable\uff09\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60\u591a\u6b21\u8c03\u7528\u51fd\u6570 f()\uff0c\u6216\u8005\u5728\u5916\u90e8\u4e5f\u5b9a\u4e49\u4e00\u4e2a\u76f8\u540c\u7684\u5217\u8868\uff0cPython \u5fc5\u987b\u4fdd\u8bc1\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u662f\u5b8c\u5168\u72ec\u7acb\u3001\u4e92\u4e0d\u5e72\u6270\u7684\u4e0d\u540c\u7684\u4e2a\u4f53\u3002<\/p>\n<ul>\n<li>\u5982\u679c Python \u5077\u61d2\uff0c\u628a [1, 2, 3] \u50cf\u6d6e\u70b9\u6570\u90a3\u6837\u63d0\u524d\u521b\u5efa\u597d\u653e\u5728\u5e38\u91cf\u8868\u91cc\uff0c\u90a3\u4e48\u6bcf\u6b21\u8c03\u7528 f() \u65f6\uff0cl1 \u90fd\u4f1a\u6307\u5411\u540c\u4e00\u4e2a\u5217\u8868\u3002<\/li>\n<li>\u8fd9\u6837\u7684\u8bdd\uff0c\u4e00\u65e6\u67d0\u6b21\u8c03\u7528\u65f6\u4fee\u6539\u4e86\u5217\u8868\uff08\u6bd4\u5982 l1.append(4)\uff09\uff0c\u5c31\u4f1a\u5f7b\u5e95\u6c61\u67d3\u9759\u6001\u5e38\u91cf\u8868\uff0c\u5bfc\u81f4\u4e0b\u4e00\u6b21\u8c03\u7528 f() \u65f6\uff0cl1 \u83ab\u540d\u5176\u5999\u53d8\u6210\u4e86 [1, 2, 3, 4]\u3002\u8fd9\u663e\u7136\u4f1a\u5e26\u6765\u707e\u96be\u6027\u7684 Bug\u3002<\/li>\n<\/ul>\n<h5>\u5c40\u90e8\u53d8\u91cf\u5728\u51fd\u6570\u8c03\u7528\u540e\u5230\u5e95\u9500\u6bc1\u7684\u662f\u4ec0\u4e48<\/h5>\n<p>\u6211\u4eec\u5bf9\u5c40\u90e8\u53d8\u91cf\u5e38\u5e38\u662f\u8fd9\u6837\u63cf\u8ff0\u7684:\n\u5c40\u90e8\u53d8\u91cf\uff08Local Variable\uff09\u662f\u6307\u5728\u51fd\u6570\uff08\u6216\u65b9\u6cd5\uff09\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf,\u5b83\u5728\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u624d\u4e34\u65f6\u521b\u5efa\uff0c\u5728\u51fd\u6570\u6267\u884c\u7ed3\u675f\u65f6\u5c31\u4f1a\u88ab\u9500\u6bc1\uff08\u5185\u5b58\u88ab\u5783\u573e\u56de\u6536\uff09\u3002<\/p>\n<p>\u8fd9\u4e2a\u63cf\u8ff0\u5176\u5b9e\u6ca1\u6709\u95ee\u9898\u7684,\u4f46\u4f3c\u4e4e\u53c8\u6709\u95ee\u9898,\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf,\u5f97\u7528\u4e00\u4e2a\u540d\u5b57\u8868\u793a\u5427,\u8fd9\u5c31\u662f\u5c40\u90e8\u53d8\u91cf\u540d,\u5b83\u6307\u5411\u4e86\u4e00\u4e2a\u5185\u5b58\u7a7a\u95f4\u3002\u5c31\u662f\u53d8\u91cf\u540d\u5f15\u7528\u4e86\u4e00\u4e2a\u5185\u5b58\u5730\u5740,\u8fd9\u4e2a\u53d8\u91cf\u540d\u5728\u5916\u90e8\u662f\u65e0\u6cd5\u4f7f\u7528\u7684,\u90a3\u51fd\u6570\u6267\u884c\u5b8c\u540epython\u505a\u4e86\u54ea\u4e9b\u52a8\u4f5c?<\/p>\n<ul>\n<li>\u5c06\u8fd9\u4e2a\u53d8\u91cf\u5f15\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u7684\u5f15\u7528\u8ba1\u6570-1\u3002<\/li>\n<li>gc\u67e5\u770b\u8fd9\u4e2a\u5185\u5b58\u7684\u5f15\u7528\u662f\u5426\u4e3a0\n<ul>\n<li>\u4e3a0: \u89e6\u53d1\u56de\u6536,\u5185\u5b58\u9500\u6bc1<\/li>\n<li>\u4e0d\u4e3a0: \u4e0d\u9500\u6bc1<\/li>\n<\/ul><\/li>\n<\/ul>\n<p>\u6240\u4ee5\u5c40\u90e8\u53d8\u91cf\u88ab\u8c03\u7528\u540e\u4f1a\u53d6\u6d88\u53d8\u91cf\u4e00\u6b21\u5f15\u7528(-1),\u662f\u5426\u9500\u6bc1\u5185\u5b58\u5b8c\u5168\u662fgc\u7684\u4e8b\u60c5<\/p>\n<h3>\u95ed\u5305<\/h3>\n<p>\u8fd9\u91cc\u6211\u4eec\u7ec8\u4e8e\u56de\u5230\u4e86\u672c\u6587\u7684\u4e3b\u9898,\u6211\u4eec\u6839\u636e\u95ed\u5305\u76843\u6761\u4ef6\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b:<\/p>\n<pre><code class=\"lang-python language-python python\">def greet(name):\n    def say():\n        print(hex(id(name)))\n        print(f&quot;\u4f60\u597d\uff0c{name}\uff01&quot;)\n    return say\n\n# 1. \u7ed9\u5b83\u53d6\u4e2a\u540d\u5b57\uff0c\u6253\u5305\u5e26\u8d70\nmy_greet = greet(&quot;\u5f20\u4e09&quot;)\n\nmy_greet()\nprint(my_greet.__closure__)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c:<\/p>\n<pre><code>0x10569e060\n\u4f60\u597d\uff0c\u5f20\u4e09\uff01\n(&lt;cell at 0x10575a590: str object at 0x10569e060&gt;,)<\/code><\/pre>\n<p>\u6211\u4eec\u5148\u4ece\u53d8\u91cf\u53ca\u5f15\u7528\u89c6\u89d2\u5206\u6790\u4e0b\u8fd9\u6bb5\u4ee3\u7801:<\/p>\n<ul>\n<li>\u6211\u4eec\u5728\u5916\u90e8\u3001\u8c03\u7528\u4e00\u4e2agreet\u51fd\u6570,\u5f53\u6267\u884cgreet()\u51fd\u6570\u540e,\u8fd4\u56de\u4e86say\u51fd\u6570\u7684\u5185\u5b58\u5730\u5740,\u5e76\u8d4b\u503c\u7ed9my_greet\u5168\u5c40\u53d8\u91cf,\u6240\u4ee5say\u51fd\u6570\u5728\u6574\u4e2a\u4ee3\u7801\u5468\u671f\u90fd\u4f1a\u6709\u5f15\u7528\u3002<\/li>\n<li>\u6211\u4eec\u5728\u6267\u884c\u4e86\u4e2agreet\u51fd\u6570\u7684\u540c\u65f6\u4f20\u9012\u4e86\u53d8\u91cf&#8221;\u5f20\u4e09&#8221;\u7ed9say\u51fd\u6570,say\u51fd\u6570\u4f1a\u5728\u5185\u5b58\u4e2d\u5f00\u8f9f\u7a7a\u95f4\u5b58\u653e\u8fd9\u4e2a&#8221;\u5f20\u4e09&#8221;,\u8fd9\u4e2a\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b,\u5185\u5b58\u4e2d\u4e0d\u4f1a\u9500\u6bc1,\u8fd9\u4e2a\u4e5f\u4e0d\u4f1a\u89e6\u53d1\u9884\u7f16\u8bd1\u7684\u9759\u6001\u5e38\u91cf\u8868,\u8fd9\u662f\u56e0\u4e3a&#8221;\u5f20\u4e09&#8221; \u662f\u8fd0\u884c\u671f\u8c03\u7528 greet(&#8220;\u5f20\u4e09&#8221;)\u52a8\u6001\u4f20\u5165\u7684\u3002<\/li>\n<li>\u65e2\u7136&#8221;\u5f20\u4e09&#8221;\u5728\u4ee3\u7801\u5468\u671f\u4e00\u76f4\u5b58\u5728,\u53ea\u8981\u6709\u5f15\u7528\u5c31\u53ef\u4ee5\u8c03\u7528,\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u53ef\u4ee5\u591a\u6b21\u901a\u8fc7my_greet()\u6253\u5370\u8fd9\u4e2a&#8221;\u5f20\u4e09&#8221;,\u90a3\u662f\u4e0d\u662fmy_greet\u6307\u5411\u7684\u5185\u5b58\u4e2d\u6709\u5bf9&#8221;\u5f20\u4e09&#8221;\u7684\u5f15\u7528?,\u4e00\u76f4\u8bb0\u5f55\u7740\u8fd9\u4e2a\u5730\u5740?<\/li>\n<\/ul>\n<pre><code class=\"lang-python language-python python\">def greet(name):\n    def say():\n        print(f&quot;\u4f60\u597d\uff0c{name}\uff01&quot;)\n    return say                  \n\n# 1. \u7ed9\u5b83\u53d6\u4e2a\u540d\u5b57\uff0c\u6253\u5305\u5e26\u8d70\nmy_greet = greet(&quot;\u5f20\u4e09&quot;)\n\nmy_greet()\nprint(my_greet.__closure__)\n\ndef greet2():\n    pass\nprint(greet2.__closure__)<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>\u4f60\u597d\uff0c\u5f20\u4e09\uff01\n(&lt;cell at 0x105592590: str object at 0x1054d6010&gt;,)\nNone<\/code><\/pre>\n<p>\u7ed3\u679c\u8868\u660e<\/p>\n<ul>\n<li>my_greet.<strong>closure<\/strong>\u6709\u4e2a\u5143\u7ec4:\n(,)<\/li>\n<\/ul>\n<pre><code>\u5168\u5c40\u53d8\u91cf my_greet \u2500\u2500(\u5f15\u7528)\u2500\u2500&gt; [ say \u51fd\u6570\u5bf9\u8c61 ]\n                                    \u2502\n                         (\u5185\u90e8\u62e5\u6709 __closure__ \u5c5e\u6027)\n                                    \u2502\n                                    \u25bc\n                        [ cell \u5bb9\u5668\u5bf9\u8c61 ] (\u7269\u7406\u5730\u5740: 0x10575a590)\n                                    \u2502\n                        (\u5185\u90e8\u6301\u6709\u4e00\u6839\u7eaf C \u8bed\u8a00\u6307\u9488)\n                                    \u2502\n                                    \u25bc\n                        [ \u5b57\u7b26\u4e32 &quot;\u5f20\u4e09&quot; ] (\u7269\u7406\u5730\u5740: 0x10569e060)\n<\/code><\/pre>\n<ul>\n<li>\u666e\u901a\u51fd\u6570\u4e5f\u6709<strong>closure<\/strong>:\u4f46\u662f\u662fNone<\/li>\n<\/ul>\n<p>\u65e2\u7136\u666e\u901a\u51fd\u6570\u6709<strong>closure<\/strong>,\u95ed\u5305\u51fd\u6570\u4f1a\u5b9e\u73b0\u4e86\u8fd9\u4e2a<strong>closure<\/strong>,\u90a3\u4e48\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u89e6\u53d1\u8fd9\u4e2a\u5b9e\u73b0<strong>closure<\/strong>?<\/p>\n<ul>\n<li>\u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u7f16\u8bd1\u5668\u5982\u679c\u53d1\u73b0\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5d4c\u5957\u4f5c\u7528\u57df\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5c31\u4f1a\u5c06\u5176\u6807\u8bb0\u4e3a co_freevars\uff08\u81ea\u7531\u53d8\u91cf\uff09\u3002<\/li>\n<li>\u5230\u4e86\u8fd0\u884c\u9636\u6bb5\uff0c\u5f53\u5916\u90e8\u51fd\u6570\u88ab\u8c03\u7528\u3001\u5185\u90e8\u51fd\u6570\u5bf9\u8c61\u88ab\u5b9e\u4f8b\u5316\u8fd4\u56de\u7684\u4e00\u77ac\u95f4\uff0cPython \u865a\u62df\u673a\u4f1a\u89e6\u53d1\u2018\u95ed\u5305\u7ed1\u5b9a\u673a\u5236\u2019\uff0c\u628a\u88c5\u6709\u5916\u90e8\u53d8\u91cf\u6307\u9488\u7684 cell \u5bf9\u8c61\u6253\u5305\u6210\u5143\u7ec4\uff0c\u76f4\u63a5\u8d4b\u503c\u7ed9\u8be5\u51fd\u6570\u5bf9\u8c61\u7684 <strong>closure<\/strong> \u5c5e\u6027 \u3002\u82e5\u65e0\u5f15\u7528\uff0c\u8be5\u5c5e\u6027\u5219\u4fdd\u6301\u9ed8\u8ba4\u7684 None\u3002\u201d<\/li>\n<\/ul>\n<p><strong><em>\u4e00\u53e5\u8bdd:\u95ed\u5305\u5c31\u662f\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\uff0c\u800c\u5185\u90e8\u51fd\u6570\u8c01\u7528\u4e86\u5916\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u672c\u8d28\u662f\u5185\u90e8\u51fd\u6570\u6709\u8fd9\u4e2a\u53d8\u91cf\u7684\u5f15\u7528<\/em><\/strong><\/p>","protected":false},"excerpt":{"rendered":"<p>\u95ed\u5305\u5c31\u662f\u80fd\u591f\u8bfb\u53d6\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u51fd\u6570 \u5f53\u4e00\u4e2a\u5d4c\u5957\u7684\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u4e14\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u4e86\u8fd9\u4e2a\u5185\u90e8\u51fd\u6570\u65f6\uff0c\u5c31\u5f62\u6210\u4e86\u95ed\u5305\u3002\u6b64\u65f6\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c&#8230;<\/p>","protected":false},"author":1,"featured_media":110,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[45],"class_list":["post-130","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"_links":{"self":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":131,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions\/131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/media\/110"}],"wp:attachment":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}