{"id":126,"date":"2026-08-04T18:06:04","date_gmt":"2026-08-04T10:06:04","guid":{"rendered":"https:\/\/www.cloudnote.cc\/?p=126"},"modified":"2026-08-04T18:06:04","modified_gmt":"2026-08-04T10:06:04","slug":"%e3%80%90python%e3%80%91%e6%b7%b1%e6%8b%b7%e8%b4%9d%e4%b8%8e%e6%b5%85%e6%8b%b7%e8%b4%9d","status":"publish","type":"post","link":"https:\/\/www.cloudnote.cc\/index.php\/2026\/08\/04\/%e3%80%90python%e3%80%91%e6%b7%b1%e6%8b%b7%e8%b4%9d%e4%b8%8e%e6%b5%85%e6%8b%b7%e8%b4%9d\/","title":{"rendered":"\u3010Python\u3011\u6df1\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d"},"content":{"rendered":"<p>\u9605\u8bfb\u672c\u7ae0\u8282\u524d,\u5efa\u8bae\u5148\u67e5\u770b <a href=\"https:\/\/www.cloudnote.cc\/index.php\/2026\/08\/04\/%e3%80%90python%e3%80%91%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e4%b8%8e%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95\/\">&#8220;\u6570\u636e\u7c7b\u578b\u4e0e\u64cd\u4f5c\u65b9\u6cd5&#8221;<\/a>\n\u5728 Python \u4e2d\uff0c\u6d45\u62f7\u8d1d\uff08Shallow Copy\uff09\u548c\u6df1\u62f7\u8d1d\uff08Deep Copy\uff09\u7684\u6838\u5fc3\u533a\u522b\u5728\u4e8e\u5bf9\u5d4c\u5957\u5bf9\u8c61\uff08\u5982\u5217\u8868\u4e2d\u7684\u5217\u8868\u3001\u5b57\u5178\u4e2d\u7684\u5217\u8868\u7b49\uff09\u7684\u5904\u7406\u65b9\u5f0f\u4e0d\u540c\u3002\n\u4e0b\u9762\u6211\u4eec\u5206\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u6765\u63a2\u8ba8:<\/p>\n<h3>int<\/h3>\n<p>\u793a\u4f8b1:<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nnum1 = 100\nnum2 = copy.copy(num1)\nnum3 = copy.deepcopy(num1)\nnum4 = num1\n\nprint(id(100), id(num1), id(num2), id(num3), id(num4))<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>4378359832 4378359832 4378359832 4378359832 4378359832<\/code><\/pre>\n<p>5\u4e2a\u7ed3\u679c\u90fd\u662f\u4e00\u6837\u7684,\u5176\u5b9e\u8fd9\u4e2a\u6bd4\u8f83\u597d\u7406\u89e3,int\u7c7b\u578b\u5c5e\u4e8e\u4e0d\u53ef\u53d8\u7c7b\u578b,100\u5728-5\uff5e256\u8303\u56f4\u5185,\u7b26\u5408\u5c0f\u6574\u6570\u7f13\u5b58\u673a\u5236,\u6240\u4ee5\u5185\u5b58\u4e2d100\u59cb\u7ec8\u53ea\u6709\u4e00\u4e2a\u5185\u5b58\u5730\u5740,\u65e0\u8bba\u6d45\u62f7\u8d1d\u8fd8\u662f\u6df1\u62f7\u8d1d,\u8fd8\u662f\u8d4b\u503c,\u90fd\u662f\u540c\u4e00\u4efd\u6570\u636e<\/p>\n<p>\u65e2\u7136\u90fd\u662f\u6307\u5411\u540c\u4e00\u4efd\u6570\u636e,\u90a3\u5982\u679c\u4fee\u6539\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u503c,\u5176\u4ed6\u662f\u5426\u4f1a\u4fee\u6539\u5462?\n\u793a\u4f8b2:<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nnum1 = 100\nnum2 = copy.copy(num1)\nnum3 = copy.deepcopy(num1)\nnum4 = num1\n\nprint(id(100), id(num1), id(num2), id(num3), id(num4))\n\nnum4 = 200\nprint(&quot;num4\u4fee\u6539\u540e\u7684id&quot;, id(num4))\nprint(id(100), id(num1), id(num2), id(num3))<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>4314560536 4314560536 4314560536 4314560536 4314560536\nnum4\u4fee\u6539\u540e\u7684id 4314563736\n4314560536 4314560536 4314560536 4314560536<\/code><\/pre>\n<p>\u7ed3\u679c\u663e\u793a\u867d\u7136num4\u4fee\u6539\u4e3a200,\u4f46\u662fnum1,num2,num3\u90fd\u662f\u4e00\u6837\u7684\u3002\u5982\u679c\u60a8\u9605\u8bfb\u8fc7\u672c\u7ad9\u5173\u4e8ePython\u7684\u7c7b\u4e0e\u5bf9\u8c61,num4=200\u53d1\u751f\u4e00\u4e0b\u64cd\u4f5c:<\/p>\n<ul>\n<li>200 \u5728\u5185\u5b58\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\n<ul>\n<li>\u5b58\u5728\n\u5c06\u5b58\u5728\u7684\u5730\u5740\u590d\u5236\u7ed9\u53d8\u91cfnum4\uff0cnum4\u5b58\u50a8\u7684\u5730\u5740\u6539\u53d8\u4e86<\/li>\n<li>\u4e0d\u5b58\u5728\n\u5f00\u8f9f\u65b0\u7684\u5185\u5b58\u7a7a\u95f4(\u5176\u5b9e\u662f\u5c0f\u5185\u5b58\u5bf9\u8c61,\u5e95\u5c42\u4e0d\u4e00\u6837\u4f1a\u771f\u6b63\u5206\u914d\u65b0\u5185\u5b58,\u4f46\u662f\u53cd\u6b63\u4f7f\u7528\u4e86\u4e0d\u4e00\u6837\u7684\u5185\u5b58),\u7136\u540e200,\u5c06\u8fd9\u4e2a\u65b0\u5185\u5b58\u5730\u5740\u8d4b\u503c\u7ed9\u53d8\u91cfnum4<\/li>\n<\/ul><\/li>\n<\/ul>\n<h3>float<\/h3>\n<p>\u793a\u4f8b3:<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nnum1 = 100.123\nnum2 = copy.copy(num1)\nnum3 = copy.deepcopy(num1)\nnum4 = num1\n\nprint(id(100.123), id(num1), id(num2), id(num3), id(num4))\n\nnum4 = 200.123\nprint(&quot;num4\u4fee\u6539\u540e\u7684id&quot;, id(num4))\nprint(id(100.123), id(num1), id(num2), id(num3))\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>4337495248 4337495248 4337495248 4337495248 4337495248\nnum4\u4fee\u6539\u540e\u7684id 4337497680\n4337495248 4337495248 4337495248 4337495248<\/code><\/pre>\n<p>flat\u4e0eint\u7c7b\u578b\u4e00\u6837<\/p>\n<h3>str<\/h3>\n<p>\u793a\u4f8b4:<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nstr1 = &quot;hello&quot;\nstr2 = copy.copy(str1)\nstr3 = copy.deepcopy(str1)\nstr4 = str1\n\nprint(id(&quot;hello&quot;), id(str1), id(str2), id(str3), id(str4))\n\nstr4 = &quot;world&quot;\nprint(&quot;str4\u4fee\u6539\u540e\u7684id&quot;, id(str4))\nprint(id(&quot;world&quot;), id(str1), id(str2), id(str3))\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n<pre><code>4307657632 4307657632 4307657632 4307657632 4307657632\nstr4\u4fee\u6539\u540e\u7684id 4308657392\n4308657392 4307657632 4307657632 4307657632<\/code><\/pre>\n<p>str\u4e0eint\u7c7b\u578b\u4e00\u6837<\/p>\n<h3>bool<\/h3>\n<p>\u793a\u4f8b5:<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nf1 = True\nf2 = copy.copy(f1)\nf3 = copy.deepcopy(f2)\nf4 = f1\n\nprint(id(True), id(f1), id(f2), id(f3), id(f4))\n\nf4 = False\nprint(&quot;f4\u4fee\u6539\u540e\u7684id&quot;, id(f4))\nprint(id(False), id(f1), id(f2), id(f3))\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n<pre><code>4337672392 4337672392 4337672392 4337672392 4337672392\nf4\u4fee\u6539\u540e\u7684id 4337672360\n4337672360 4337672392 4337672392 4337672392<\/code><\/pre>\n<p>bool\u4e0eint\u7c7b\u578b\u4e00\u6837<\/p>\n<h3>tuple<\/h3>\n<p>\u793a\u4f8b6:<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nt1 = (&quot;name&quot;, &quot;age&quot;)\nt2 = copy.copy(t1)\nt3 = copy.deepcopy(t2)\nt4 = t1\n\nprint(id(True), id(t1), id(t2), id(t3), id(t4))\n\nt4 = (&quot;name2&quot;, &quot;age2&quot;)\nprint(&quot;t4\u4fee\u6539\u540e\u7684id&quot;, id(t4))\nprint(id(False), id(t1), id(t2), id(t3))<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n<pre><code>4303790280 4306480128 4306480128 4306480128 4306480128\nt4\u4fee\u6539\u540e\u7684id 4306565952\n4303790248 4306480128 4306480128 4306480128<\/code><\/pre>\n<p>tuple\u4e0eint\u7c7b\u578b\u4e00\u6837<\/p>\n<h3>list\/set\/dict<\/h3>\n<p>list\u5c5e\u4e8e\u53ef\u53d8\u7c7b\u578b,\u6709\u4e2d\u95f4\u8868\n\u793a\u4f8b7(\u4e0d\u53ef\u53d8\u5143\u7d20\u5217\u8868):<\/p>\n<pre><code class=\"lang-python language-python python\">\nimport copy\n\nlist1 = [1, 2, 3, 4, 5, 6, 7, 8]\nlist2 = copy.copy(list1)\nlist3 = copy.deepcopy(list1)\nlist4 = list1\n\nprint(id(list1), id(list2), id(list3), id(list4))\nprint(&quot;list1=&quot;, list1, &quot;list2=&quot;, list2,  &quot;list3=&quot;, list3, &quot;list4=&quot;, list4)\n\nlist1[0] = 100\nprint(id(list1), id(list2), id(list3), id(list4))\nprint(&quot;list1=&quot;, list1, &quot;list2=&quot;, list2,  &quot;list3=&quot;, list3, &quot;list4=&quot;, list4)\n\nlist2[1] = 200\nprint(id(list1), id(list2), id(list3), id(list4))\nprint(&quot;list1=&quot;, list1, &quot;list2=&quot;, list2,  &quot;list3=&quot;, list3, &quot;list4=&quot;, list4)\n\nlist3[2] = 300\nprint(id(list1), id(list2), id(list3), id(list4))\nprint(&quot;list1=&quot;, list1, &quot;list2=&quot;, list2,  &quot;list3=&quot;, list3, &quot;list4=&quot;, list4)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>4311502912 4311501120 4313167744 4311502912\nlist1= [1, 2, 3, 4, 5, 6, 7, 8] list2= [1, 2, 3, 4, 5, 6, 7, 8] list3= [1, 2, 3, 4, 5, 6, 7, 8] list4= [1, 2, 3, 4, 5, 6, 7, 8]\n4311502912 4311501120 4313167744 4311502912\nlist1= [100, 2, 3, 4, 5, 6, 7, 8] list2= [1, 2, 3, 4, 5, 6, 7, 8] list3= [1, 2, 3, 4, 5, 6, 7, 8] list4= [100, 2, 3, 4, 5, 6, 7, 8]\n4311502912 4311501120 4313167744 4311502912\nlist1= [100, 2, 3, 4, 5, 6, 7, 8] list2= [1, 200, 3, 4, 5, 6, 7, 8] list3= [1, 2, 3, 4, 5, 6, 7, 8] list4= [100, 2, 3, 4, 5, 6, 7, 8]\n4311502912 4311501120 4313167744 4311502912\nlist1= [100, 2, 3, 4, 5, 6, 7, 8] list2= [1, 200, 3, 4, 5, 6, 7, 8] list3= [1, 2, 300, 4, 5, 6, 7, 8] list4= [100, 2, 3, 4, 5, 6, 7, 8]<\/code><\/pre>\n<p>\u6211\u4eec\u603b\u7ed3\u4e0b\u8fd0\u884c\u7ed3\u679c:<\/p>\n<ul>\n<li>\u6539\u53d8list\u7684\u8868\u7684\u5185\u5bb9,\u5f15\u7528\u5b83\u7684\u53d8\u91cf\u5185\u5b58\u5730\u5740\u6c38\u8fdc\u4e0d\u53d8\u3002<\/li>\n<li>\u8d4b\u503c\u64cd\u4f5c\u53d8\u91cflist4\u7684\u5185\u5b58\u5730\u5740\u59cb\u7ec8\u7b49\u4e8elist1,\u8bf4\u660elist1\u4e0elist4\u59cb\u7ec8\u6307\u5411\u540c\u4e00\u5feb\u5185\u5b58\u5730\u5740,\u53ef\u4ee5\u4e00\u4e2a\u4eba\u6709\u4e24\u4e2a\u540d\u5b57,\u6240\u4ee5list1\u4fee\u6539\u540elist4\u4e5f\u53d1\u751f\u4e86\u53d8\u5316<\/li>\n<li>\u6d45\u62f7\u8d1d\u4e0e\u6df1\u62f7\u8d1d\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u90fd\u53d1\u751f\u4e86\u53d8\u5316\n*\u4e09\u4e2a\u5217\u8868\u4fee\u6539\u4e92\u76f8\u6ca1\u6709\u5f71\u54cd,\u4fee\u6539\u540e\u8bf4\u660e\u4e2d\u95f4\u90fd\u6ca1\u6709\u6536\u5230\u5f71\u54cd,\u81f3\u5c11\u8bf4\u660e\u4e2d\u95f4\u8868\u662f\u72ec\u7acb\u7684\u3002\u5728\u672a\u4f5c\u4efb\u4f55\u4fee\u6539\u524d\u8f93\u51fa\u503c\u90fd\u4e00\u6837:\u8bf4\u660e\u4e2d\u95f4\u8868\u90fd\u662f\u590d\u5236\u4e8elist1<\/li>\n<\/ul>\n<p>\u6211\u4eec\u603b\u7ed3\u51fa\u6df1\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d\u4e24\u4e2a\u7ed3\u8bba:<\/p>\n<ul>\n<li>\u5217\u8868\u5143\u7c7b\u6570\u636e\u88ab\u590d\u5236,\u5373\u5217\u8868\u53d8\u91cf\u6307\u5411\u7684\u5185\u5b58\u5730\u5740\u6539\u53d8\u4e86,\u8d4b\u7ed9\u4e86\u65b0\u53d8\u91cflist2\u4e0elist3,\u6240\u4ee5id(list2)\u53caid(list3)\u4e0eid(list1)\u503c\u4e0d\u540c\u3002<\/li>\n<li>\u4e2d\u95f4\u8868\u88ab\u590d\u5236\u4e86,\u56e0\u4e3a\u90fd\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b,\u5c31\u7b97\u72ec\u7acb\u7a7a\u95f4,\u6240\u6709\u76f8\u540c\u503c\u7684\u6570\u5b57\u5730\u5740\u90fd\u4f1a\u4e00\u6837,\u53ea\u80fd\u786e\u8ba4\u5230\u4e2d\u95f4\u8868\u88ab\u590d\u5236\u4e86,\u90fd\u5728\u5185\u5b58\u4e2d\u6709\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\u5b58\u653e,\u5982\u679c\u4e00\u6837\u80af\u5b9a\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b8(\u542b\u6709\u53ef\u53d8\u5143\u7d20\u7684\u5217\u8868):<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nlist1 = [1, [20, 21, 23], 3]\nlist2 = copy.copy(list1)\nlist3 = copy.deepcopy(list1)\nlist4 = list1\n\nprint(list1[1] is list2[1])\n\nprint(list1[1] is list3[1])\n\nprint(id(list1), id(list2), id(list3), id(list4))\nprint(&quot;list1=&quot;, list1, &quot;list2=&quot;, list2,  &quot;list3=&quot;, list3, &quot;list4=&quot;, list4)\n\nlist1[1][0] = 100\nprint(id(list1), id(list2), id(list3), id(list4))\nprint(&quot;list1=&quot;, list1, &quot;list2=&quot;, list2,  &quot;list3=&quot;, list3, &quot;list4=&quot;, list4)\n\nlist2[1][1] = 200\nprint(id(list1), id(list2), id(list3), id(list4))\nprint(&quot;list1=&quot;, list1, &quot;list2=&quot;, list2,  &quot;list3=&quot;, list3, &quot;list4=&quot;, list4)\n\nlist3[1][2] = 300\nprint(id(list1), id(list2), id(list3), id(list4))\nprint(&quot;list1=&quot;, list1, &quot;list2=&quot;, list2,  &quot;list3=&quot;, list3, &quot;list4=&quot;, list4)<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>True\nFalse\n4375939392 4376377216 4377182784 4375939392\nlist1= [1, [20, 21, 23], 3] list2= [1, [20, 21, 23], 3] list3= [1, [20, 21, 23], 3] list4= [1, [20, 21, 23], 3]\n4375939392 4376377216 4377182784 4375939392\nlist1= [1, [100, 21, 23], 3] list2= [1, [100, 21, 23], 3] list3= [1, [20, 21, 23], 3] list4= [1, [100, 21, 23], 3]\n4375939392 4376377216 4377182784 4375939392\nlist1= [1, [100, 200, 23], 3] list2= [1, [100, 200, 23], 3] list3= [1, [20, 21, 23], 3] list4= [1, [100, 200, 23], 3]\n4375939392 4376377216 4377182784 4375939392\nlist1= [1, [100, 200, 23], 3] list2= [1, [100, 200, 23], 3] list3= [1, [20, 21, 300], 3] list4= [1, [100, 200, 23], 3]<\/code><\/pre>\n<p>\u6211\u4eec\u5df2\u7ecf\u5206\u6790\u793a\u4f8b7\u786e\u8ba4\u4e2d\u95f4\u8868\u90fd\u4f1a\u590d\u5236\u8fc7\u6765\u3002\u4f46\u662f\u5982\u679c\u5217\u8868\u4e2d\u542b\u6709\u53ef\u53d8\u7c7b\u578b\u65f6:\u6211\u4eec\u53ef\u53d8\u7c7b\u578b,\u867d\u7136\u5217\u8868\u503c\u5b8c\u5168\u4e00\u6837,\u4f46\u662f\u53ef\u4ee5\u91cd\u65b0\u5f00\u8f9f\u5185\u5b58\u7a7a\u95f4\u5b58\u653e\u7684\u3002\u4e2d\u95f4\u8868\u662f\u5185\u5b58\u8fde\u7eed\u7684\u6307\u9488\u6570\u7ec4,\u5b58\u653e\u7684\u662f\u5bf9\u5e94\u6570\u636e\u7684\u5185\u5b58\u5730\u5740,\u90a3\u4e48\u53ef\u53d8\u7c7b\u578b\u7684\u5185\u662f\u5426\u4f1a\u90fd\u4e00\u6837\u7684\u5462?\n\u524d\u4e24\u884c\u7684\u6253\u5370\u7ed3\u679c\u5df2\u7ecf\u8868\u660e:<\/p>\n<ul>\n<li>\u6d45\u62f7\u8d1d\u51fa\u6765\u7684list2\u4e2d\u5b58\u653e\u7684\u53ef\u53d8\u7c7b\u578b\u7c7b\u8868[20, 21, 23]\u5730\u5740\u4e0elist1\u5b8c\u5168\u4e00\u6837\u3002\u6240\u4ee5list2\u7684\u4e2d\u95f4\u8868\u662f\u5b8c\u6574\u5730\u4ecelist1\u62f7\u8d1d\u8fc7\u6765\u7684\u3002<\/li>\n<li>\u6df1\u62f7\u8d1d\u51fa\u6765\u7684list3\u4e2d\u5b58\u653e\u7684\u53ef\u53d8\u7c7b\u578b\u7c7b\u8868[20, 21, 23]\u5730\u5740\u4e0elist1\u4e0d\u4e00\u6837\u3002\u6240\u4ee5list3\u7684\u4e2d\u95f4\u8868\u662f\u4ecelist1\u62f7\u8d1d\u8fc7\u6765\u7684,\u4f46\u662f\u9047\u5230\u53ef\u53d8\u7c7b\u578b\u4f1a\u91cd\u65b0\u5f00\u8f9f\u7a7a\u95f4,\u5c06\u6570\u636e\u590d\u5236\u8fc7\u6765\u540e\u518d\u5c06\u65b0\u5730\u5740\u8986\u76d6\u539f\u6765\u7684\u503c<\/li>\n<\/ul>\n<p>\u65e2\u7136\u4e0d\u53ef\u91cf\u7c7b\u578b\u5927\u5bb6\u90fd\u4e00\u6837,\u6df1\u62f7\u8d1d\u4f1a\u5f00\u8f9f\u72ec\u7acb\u5185\u5b58\u5b58\u653e\u53ef\u53d8\u7c7b\u578b\u6570\u636e,\u90a3\u6df1\u62f7\u8d1d\u5c31\u662f\u5b8c\u5168\u72ec\u7acb\u7684\u6570\u636e\u3002\u800c\u6d45\u62f7\u8d1d\u4f1a\u4fdd\u7559\u53ef\u53d8\u7c7b\u578b\u6570\u636e\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<p>\u73b0\u5728\u53ef\u4ee5\u505a\u6700\u7ec8\u603b\u7ed3\u4e86:<\/p>\n<ul>\n<li>\u6d45\u62f7\u8d1d:\n<ul>\n<li>\u62f7\u8d1d\u5143\u6570\u636e,\u5b58\u653e\u72ec\u7acb\u5185\u5b58\u7a7a\u95f4<\/li>\n<li>\u62f7\u8d1d\u4e2d\u95f4\u8868,\u5b58\u653e\u72ec\u7acb\u7a7a\u95f4,\u4e2d\u95f4\u8868\u4e0d\u4fee\u6539<\/li>\n<\/ul><\/li>\n<li>\u6df1\u62f7\u8d1d:\n<ul>\n<li>\u62f7\u8d1d\u5143\u6570\u636e,\u5b58\u653e\u72ec\u7acb\u5185\u5b58\u7a7a\u95f4<\/li>\n<li>\u62f7\u8d1d\u4e2d\u95f4\u8868,\u5b58\u653e\u72ec\u7acb\u7a7a\u95f4,\u4e2d\u95f4\u8868\u5bf9\u53ef\u53d8\u7c7b\u578b\u6570\u636e\u5f00\u8f9f\u65b0\u5185\u5b58\u5b58\u653e,\u5176\u4f59\u4e0d\u53d8<\/li>\n<\/ul><\/li>\n<li>\u8d4b\u503c:\n\u7ed9\u539f\u6765\u7684\u53d8\u91cf\u52a0\u4e00\u4e2a\u65b0\u540d\u79f0,\u5c31\u662f\u540c\u4e00\u4e2a<\/li>\n<\/ul>\n<p>\u5176\u4f59\u8fd0\u884c\u7ed3\u679c\u5b8c\u5168\u7b26\u5408\u6211\u4eec\u7684\u7ed3\u8bba\u3002<\/p>\n<pre><code>===========================================================================================================\n                                 \u3010 Python \u8d4b\u503c vs \u6d45\u62f7\u8d1d vs \u6df1\u62f7\u8d1d \u5185\u5b58\u5168\u666f\u56fe \u3011\n===========================================================================================================\n\n \u3010 \u6808\u533a (Stack) \u3011             \u3010 \u5806\u533a (Heap) &mdash;&mdash; \u7b2c\u4e00\u3001\u4e8c\u5c42\uff1a\u5916\u58f3\u5143\u6570\u636e\u4e0e\u4e2d\u95f4\u8868 \u3011                     \u3010 \u5806\u533a (Heap) &mdash;&mdash; \u7b2c\u4e09\u5c42\uff1a\u771f\u5b9e\u6570\u636e\u5b9e\u4f53 \u3011\n   \u53d8\u91cf\u540d (\u6307\u9488)\n                                                                                            \u250c\u2500\u2500\u2500\u25ba [ \u4e0d\u53ef\u53d8\u5c0f\u6574\u6570 1 ] (\u5168\u5c40\u5355\u4f8b\u6c60\uff0c\u5168\u5929\u4e0b\u590d\u7528)\n    list1 \u2500\u2500\u2500\u2510                                                                              \u2502\n             \u251c\u2500\u2500\u2500\u25ba \u3010 \u72ec\u7acb\u5185\u5b58: \u5143\u6570\u636e A \u3011 \u2500\u2500\u2500\u25ba \u3010 \u72ec\u7acb\u5185\u5b58: \u4e2d\u95f4\u8868 A \u3011 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n    list4 \u2500\u2500\u2500\u2518      - \u8bb0\u5f55 ma_used=3                    - 0\u53f7\u683c\u5b50: [ \u6307\u9488 ]                 \u2502\n   (\u2605 \u8d4b\u503c:          - \u8bb0\u5f55 \u4e2d\u95f4\u8868A\u5730\u5740                  - 1\u53f7\u683c\u5b50: [ \u6307\u9488 \u2500\u2500\u25ba 0xAAAA ] \u2500\u2510   \u2514\u2500\u2500\u2500\u25ba [ \u4e0d\u53ef\u53d8\u5c0f\u6574\u6570 3 ] (\u5168\u5c40\u5355\u4f8b\u6c60\uff0c\u5168\u5929\u4e0b\u590d\u7528)\n    \u540c\u4e2a\u5916\u58f3\u591a\u8d34                                        - 2\u53f7\u683c\u5b50: [ \u6307\u9488 ]             \u2502\n    \u4e00\u4e2a\u522b\u540d\u6807\u7b7e)                                                                       \u2502\n                                                                                        \u2502\n                                                                                        \u25bc\n    list2 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25ba \u3010 \u72ec\u7acb\u5185\u5b58: \u5143\u6570\u636e B \u3011 \u2500\u2500\u2500\u25ba \u3010 \u72ec\u7acb\u5185\u5b58: \u4e2d\u95f4\u8868 B \u3011                \u3010 \u65e7\u5d4c\u5957\u5217\u8868\u5b9e\u4f53 (\u5185\u5b58\u5730\u5740: 0xAAAA) \u3011\n   (\u2605 \u6d45\u62f7\u8d1d:        - \u8bb0\u5f55 ma_used=3                    - 0\u53f7\u683c\u5b50: [ \u6307\u9488 ] \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524   | - 0\u53f7\u683c\u5b50: [ \u6307\u9488 \u2500\u2500\u25ba 20 ]\n    \u72ec\u7acb\u5f00\u8f9f\u4e86          - \u8bb0\u5f55 \u4e2d\u95f4\u8868B\u5730\u5740                  - 1\u53f7\u683c\u5b50: [ \u6307\u9488 \u2500\u2500\u25ba 0xAAAA ] \u2500\u2518   | - 1\u53f7\u683c\u5b50: [ \u6307\u9488 \u2500\u2500\u25ba 21 ]\n    \u4e24\u5757\u65b0\u5185\u5b58\uff0c                                        - 2\u53f7\u683c\u5b50: [ \u6307\u9488 ]                 +---------------------------------------------+\n    \u4f46\u5185\u90e8\u6307\u9488\u76f4\u63a5                                                                             \u25b2 (\u2605 \u6d45\u62f7\u8d1d\u5171\u4eab\u8fd9\u91cc\uff0c\u6539\u52a8\u5185\u5c42\u4f1a\u5f15\u7206\u8054\u52a8\u707e\u96be\uff01)\n    \u590d\u5236\u65e7\u5730\u5740)                                                                                \u2502\n                                                                                               \u2502\n                                                                                               \u2502\n    list3 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25ba \u3010 \u72ec\u7acb\u5185\u5b58: \u5143\u6570\u636e C \u3011 \u2500\u2500\u2500\u25ba \u3010 \u72ec\u7acb\u5185\u5b58: \u4e2d\u95f4\u8868 C \u3011 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 (\u2605 \u6df1\u62f7\u8d1d\u9047\u5230\u4e0d\u53ef\u53d8\u6574\u6570\uff0c\u89e6\u53d1\u514d\u6b7b\u91d1\u724c\u9000\u5316\u590d\u7528)\n   (\u2605 \u6df1\u62f7\u8d1d:        - \u8bb0\u5f55 ma_used=3                    - 0\u53f7\u683c\u5b50: [ \u6307\u9488 ] \n    \u5f00\u8f9f\u5168\u65b0\u8868C\uff0c      - \u8bb0\u5f55 \u4e2d\u95f4\u8868C\u5730\u5740                  - 1\u53f7\u683c\u5b50: [ \u6307\u9488 \u2500\u2500\u25ba 0xZZZZ ] \u2500\u2510\n    \u9047\u5230\u53ef\u53d8\u5217\u8868                                        - 2\u53f7\u683c\u5b50: [ \u6307\u9488 ]             \u2502\n    \u5f7b\u5e95\u5728\u5e95\u5c42\u9020                                                                        \u25bc\n    \u5b8c\u5168\u9694\u79bb\u7684\u65b0\u623f)                                                                  \u3010 \u65b0\u5d4c\u5957\u5217\u8868\u5b9e\u4f53 (\u5185\u5b58\u5730\u5740: 0xZZZZ) \u3011\n                                                                                      | - 0\u53f7\u683c\u5b50: [ \u6307\u9488 \u2500\u2500\u25ba 20 ] (\u7269\u7406\u7edd\u5bf9\u9694\u79bb\u7684\u65b0\u4e16\u754c)\n                                                                                      | - 1\u53f7\u683c\u5b50: [ \u6307\u9488 \u2500\u2500\u25ba 21 ]\n                                                                                      +---------------------------------------------+\n===========================================================================================================\n<\/code><\/pre>\n<p>\u6211\u4eec\u8d34\u4e00\u4e0b\u6e90\u7801\u4e2d\u63cf\u8ff0<\/p>\n<pre><code>The difference between shallow and deep copying is only relevant for\ncompound objects (objects that contain other objects, like lists or\nclass instances).\n\n- A shallow copy constructs a new compound object and then (to the\n  extent possible) inserts *the same objects* into it that the\n  original contains.\n\n- A deep copy constructs a new compound object and then, recursively,\n  inserts *copies* into it of the objects found in the original.\n\nTwo problems often exist with deep copy operations that don't exist\nwith shallow copy operations:\n\n a) recursive objects (compound objects that, directly or indirectly,\n    contain a reference to themselves) may cause a recursive loop\n\n b) because deep copy copies *everything* it may copy too much, e.g.\n    administrative data structures that should be shared even between\n    copies\n\nPython's deep copy operation avoids these problems by:\n\n a) keeping a table of objects already copied during the current\n    copying pass\n\n b) letting user-defined classes override the copying operation or the\n    set of components copied\n\nThis version does not copy types like module, class, function, method,\nnor stack trace, stack frame, nor file, socket, window, nor any\nsimilar types.\n\nClasses can use the same interfaces to control copying that they use\nto control pickling: they can define methods called __getinitargs__(),\n__getstate__() and __setstate__().  See the documentation for module\n&quot;pickle&quot; for information on these methods.<\/code><\/pre>\n<p>\u5de5\u5177\u7b80\u5355\u7ffb\u4e00\u4e0b:<\/p>\n<pre><code>===========================================================================================================\n                               \u3010 Python copy \u6a21\u5757\uff1a\u901a\u7528\uff08\u6d45\u62f7\u8d1d\u4e0e\u6df1\u62f7\u8d1d\uff09\u64cd\u4f5c\u8bf4\u660e \u3011\n===========================================================================================================\n\n\u63a5\u53e3\u6458\u8981\uff08\u4f7f\u7528\u7b80\u4ecb\uff09\uff1a\n-----------------------------------------------------------------------------------------------------------\n        import copy\n\n        x = copy.copy(y)        # \u5bf9\u5bf9\u8c61 y \u8fdb\u884c\u3010\u6d45\u62f7\u8d1d\u3011\n        x = copy.deepcopy(y)    # \u5bf9\u5bf9\u8c61 y \u8fdb\u884c\u3010\u6df1\u62f7\u8d1d\u3011\n\n\u5982\u679c\u8981\u5f15\u53d1\u6a21\u5757\u7279\u6709\u7684\u9519\u8bef\uff0c\u4f1a\u629b\u51fa copy.Error \u5f02\u5e38\u3002\n\n\u3010\u6838\u5fc3\u5206\u6c34\u5cad\u3011\uff1a\n\u6d45\u62f7\u8d1d\u4e0e\u6df1\u62f7\u8d1d\u7684\u533a\u522b\uff0c\u4ec5\u4ec5\u5728\u9762\u5bf9\u3010\u7ec4\u5408\u5bf9\u8c61 \/ \u590d\u5408\u5bf9\u8c61\u3011\uff08\u5373\u5185\u90e8\u5305\u542b\u5176\u4ed6\u5bf9\u8c61\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5b57\u5178\u6216\u7c7b\u5b9e\u4f8b\uff09\u65f6\u624d\u5177\u6709\u5b9e\u9645\u610f\u4e49\u3002\n\n- \u3010\u6d45\u62f7\u8d1d\uff08Shallow Copy\uff09\u3011\uff1a\n  \u4f1a\u5728\u5185\u5b58\u4e2d\u6784\u9020\u51fa\u4e00\u4e2a\u5168\u65b0\u7684\u7ec4\u5408\u5bf9\u8c61\uff08\u5373\u5f00\u8f9f\u65b0\u5916\u58f3\u3001\u65b0\u4e2d\u95f4\u8868\uff09\uff0c\u7136\u540e\uff08\u5728\u53ef\u80fd\u7684\u6700\u5927\u8303\u56f4\u5185\uff09\u5c06\u539f\u5bb9\u5668\u4e2d\u6240\u5305\u542b\u7684\u3010\u540c\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f53\u3011\u7684\u6307\u9488\u5730\u5740\u76f4\u63a5\u585e\u5165\u5176\u4e2d\u3002\n\n- \u3010\u6df1\u62f7\u8d1d\uff08Deep Copy\uff09\u3011\uff1a\n  \u4f1a\u5728\u5185\u5b58\u4e2d\u6784\u9020\u51fa\u4e00\u4e2a\u5168\u65b0\u7684\u7ec4\u5408\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7\u3010\u9012\u5f52\uff08\u5c42\u5c42\u8ffd\u6740\uff09\u3011\u7684\u65b9\u5f0f\uff0c\u5c06\u539f\u5bb9\u5668\u4e2d\u627e\u5230\u7684\u6240\u6709\u5b50\u5bf9\u8c61\u5b9e\u4f53\u7684\u3010\u5168\u65b0\u514b\u9686\u526f\u672c\u3011\u585e\u5165\u5176\u4e2d\u3002\n\n-----------------------------------------------------------------------------------------------------------\n\u3010\u6df1\u62f7\u8d1d\u9762\u4e34\u7684\u4e24\u5927\u7ecf\u5178\u7269\u7406\u75db\u70b9\u3011\uff08\u6d45\u62f7\u8d1d\u5929\u7136\u4e0d\u5b58\u5728\u8fd9\u4e24\u4e2a\u95ee\u9898\uff09\uff1a\n-----------------------------------------------------------------------------------------------------------\n a) \u3010\u5faa\u73af\u5f15\u7528 \/ \u9012\u5f52\u5bf9\u8c61\u3011\uff1a\n    \u6709\u4e9b\u7ec4\u5408\u5bf9\u8c61\u4f1a\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5305\u542b\u4e00\u4e2a\u6307\u5411\u5b83\u81ea\u5df1\u7684\u6307\u9488\u7ebf\u3002\u8fd9\u5728\u6df1\u62f7\u8d1d\u5c42\u5c42\u9012\u5f52\u65f6\uff0c\u6781\u6613\u5bfc\u81f4\u7a0b\u5e8f\u5760\u5165\u6b7b\u5faa\u73af\uff08\u7206\u6808\uff09\u3002\n\n b) \u3010\u62f7\u8d1d\u8fc7\u8f7d \/ \u590d\u5236\u8fc7\u591a\u3011\uff1a\n    \u56e0\u4e3a\u6df1\u62f7\u8d1d\u9ed8\u8ba4\u4f1a\u65e0\u8111\u5730\u53bb\u514b\u9686&ldquo;\u89c6\u7ebf\u8303\u56f4\u5185\u7684\u6240\u6709\u4e1c\u897f&rdquo;\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5b83\u590d\u5236\u4e86\u4e0d\u8be5\u590d\u5236\u7684\u5143\u6570\u636e\u3002\u4f8b\u5982\uff1a\u4e00\u4e9b\u672c\u5e94\u8be5\u5728\u591a\u4e2a\u526f\u672c\u4e4b\u95f4\u5f3a\u884c\u5171\u4eab\u7684\u7ba1\u7406\u578b\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002\n\n-----------------------------------------------------------------------------------------------------------\n\u3010Python \u5b98\u65b9\u6df1\u62f7\u8d1d\u5f15\u64ce\u662f\u5982\u4f55\u6d88\u706d\u8fd9\u4e24\u4e2a\u75db\u70b9\u7684\uff1f\u3011\uff1a\n-----------------------------------------------------------------------------------------------------------\n a) \u3010\u8bb0\u8d26\u96f7\u8fbe\u673a\u5236 (memo \u5b57\u5178)\u3011\uff1a\n    \u5728\u5f53\u524d\u7684\u5355\u6b21\u62f7\u8d1d\u642c\u5bb6\u8fc7\u7a0b\u4e2d\uff0c\u5e95\u5c42\u4f1a\u6b7b\u6b7b\u7ef4\u62a4\u4e00\u5f20&ldquo;\u5df2\u7ecf\u62f7\u8d1d\u8fc7\u7684\u5bf9\u8c61 ID \u767b\u8bb0\u8868&rdquo;\u3002\u4e00\u65e6\u53d1\u73b0\u67d0\u4e2a\u5bf9\u8c61\u5df2\u7ecf\u9020\u8fc7\u65b0\u623f\u5b50\u4e86\uff0c\u76f4\u63a5\u590d\u7528\uff0c\u5f7b\u5e95\u6d88\u706d\u6b7b\u5faa\u73af\u3002\n\n b) \u3010\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u91cd\u5199\u3011\uff1a\n    \u5141\u8bb8\u7528\u6237\u5728\u81ea\u5efa\u7684\u7c7b\uff08Class\uff09\u4e2d\uff0c\u901a\u8fc7\u91cd\u5199\u7279\u5b9a\u7684\u65b9\u6cd5\u6765\u63a5\u7ba1\u62f7\u8d1d\u52a8\u4f5c\uff0c\u6216\u8005\u81ea\u7531\u6307\u5b9a\u5230\u5e95\u6709\u54ea\u4e9b\u5185\u90e8\u7ec4\u4ef6\u9700\u8981\u88ab\u62f7\u8d1d\u3002\n\n\u6ce8\u610f\uff1a\u5f53\u524d\u7248\u672c\u7684\u6df1\u62f7\u8d1d\u5f15\u64ce\u3010\u9ed8\u8ba4\u4e0d\u4f1a\u53bb\u62f7\u8d1d\u3011\u4ee5\u4e0b\u7c7b\u578b\uff1a\n\u6a21\u5757\uff08module\uff09\u3001\u7c7b\u5bf9\u8c61\uff08class\uff09\u3001\u51fd\u6570\uff08function\uff09\u3001\u65b9\u6cd5\uff08method\uff09\u3001\u5806\u6808\u8ffd\u8e2a\u8bb0\u5f55\uff08stack trace\uff09\u3001\u5806\u6808\u5e27\uff08stack frame\uff09\u3001\u4ee5\u53ca\u6587\u4ef6\uff08file\uff09\u3001\u5957\u63a5\u5b57\uff08socket\uff09\u3001\u7a97\u53e3\uff08window\uff09\u6216\u4efb\u4f55\u7c7b\u4f3c\u7684\u5e95\u5c42\u7cfb\u7edf\u7ea7\u7c7b\u578b\u3002\n\n-----------------------------------------------------------------------------------------------------------\n\u3010\u9ad8\u7ea7\u81ea\u5b9a\u4e49\u63a7\u5236\u6307\u5357\u3011\uff1a\n-----------------------------------------------------------------------------------------------------------\n\u5f00\u53d1\u8005\u5728\u81ea\u5b9a\u4e49\u7684\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0e\u63a7\u5236&ldquo;\u5bf9\u8c61\u5e8f\u5217\u5316\uff08pickle\uff09&rdquo;\u5b8c\u5168\u76f8\u540c\u7684\u63a5\u53e3\u6765\u63a5\u7ba1\u548c\u63a7\u5236\u62f7\u8d1d\u884c\u4e3a\uff1a\n\u4ed6\u4eec\u53ef\u4ee5\u4e3a\u7c7b\u5b9a\u4e49\u540d\u4e3a `__getinitargs__()`\u3001`__getstate__()` \u548c `__setstate__()` \u7684\u9b54\u672f\u65b9\u6cd5\u3002\u5177\u4f53\u7ec6\u8282\u53ef\u4ee5\u53bb\u7ffb\u9605 &quot;pickle&quot; \u6807\u51c6\u5e93\u6a21\u5757\u7684\u5b98\u65b9\u6587\u6863\u3002\n===========================================================================================================\n<\/code><\/pre>\n<p>dict\u90fd\u6709\u5143\u6570\u636e,\u4e2d\u95f4\u8868\uff0c\u6240\u4ee5dict\u53caset\u4e0elist\u7684\u6d45\u62f7\u8d1d\u3001\u6df1\u62f7\u8d1d\u3001\u8d4b\u503c\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u78019(value\u4e0d\u53ef\u53d8\u7c7b\u578b):<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nd1 = {&quot;k1&quot;: &quot;v1&quot;, &quot;k2&quot;: &quot;v2&quot;, &quot;k3&quot;: &quot;v3&quot;}\nd2 = copy.copy(d1)\nd3 = copy.deepcopy(d1)\nd4 = d1\n\nprint(id(d1), id(d2), id(d3), id(d4))\nprint(f&quot;{'-' * 16}\u672a\u4fee\u6539\u6570\u636e{'-' * 16}&quot;)\nprint(&quot;d1=&quot;, d1, &quot;\\nd2=&quot;, d2, &quot;\\nd3=&quot;, d3, &quot;\\nd4=&quot;, d4)\n\nd1[&quot;k1&quot;] = &quot;v11&quot;\nprint(f&quot;{'-' * 16}\u4fee\u6539d1\u7684k1\u6570\u636e{'-' * 16}&quot;)\nprint(id(d1), id(d2), id(d3), id(d4))\nprint(&quot;d1=&quot;, d1, &quot;\\nd2=&quot;, d2, &quot;\\nd3=&quot;, d3, &quot;\\nd4=&quot;, d4)\n\nd2[&quot;k2&quot;] = &quot;v22&quot;\nprint(f&quot;{'-' * 16}\u4fee\u6539d2\u7684k2\u6570\u636e{'-' * 16}&quot;)\nprint(id(d1), id(d2), id(d3), id(d4))\nprint(&quot;d1=&quot;, d1, &quot;\\nd2=&quot;, d2, &quot;\\nd3=&quot;, d3, &quot;\\nd4=&quot;, d4)\n\nd3[&quot;k3&quot;] = &quot;v33&quot;\nprint(f&quot;{'-' * 16}\u4fee\u6539d3\u7684k3\u6570\u636e{'-' * 16}&quot;)\nprint(id(d1), id(d2), id(d3), id(d4))\nprint(&quot;d1=&quot;, d1, &quot;\\nd2=&quot;, d2, &quot;\\nd3=&quot;, d3, &quot;\\nd4=&quot;, d4)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>4310962816 4310010688 4311006400 4310962816\nd1= {'k1': 'v11', 'k2': 'v2', 'k3': 'v3'} \nd2= {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'} \nd3= {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'} \nd4= {'k1': 'v11', 'k2': 'v2', 'k3': 'v3'}\n----------------\u4fee\u6539d2\u7684k2\u6570\u636e----------------\n4310962816 4310010688 4311006400 4310962816\nd1= {'k1': 'v11', 'k2': 'v2', 'k3': 'v3'} \nd2= {'k1': 'v1', 'k2': 'v22', 'k3': 'v3'} \nd3= {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'} \nd4= {'k1': 'v11', 'k2': 'v2', 'k3': 'v3'}\n----------------\u4fee\u6539d3\u7684k3\u6570\u636e----------------\n4310962816 4310010688 4311006400 4310962816\nd1= {'k1': 'v11', 'k2': 'v2', 'k3': 'v3'} \nd2= {'k1': 'v1', 'k2': 'v22', 'k3': 'v3'} \nd3= {'k1': 'v1', 'k2': 'v2', 'k3': 'v33'} \nd4= {'k1': 'v11', 'k2': 'v2', 'k3': 'v3'}<\/code><\/pre>\n<p>\u7ed3\u679c\u8868\u73b0\u4e0elist\u5b8c\u5168\u4e00\u81f4\ndict\u4e0elist\u4e0d\u540c\u70b9\u5728\u4e8e\u4e2d\u95f4\u8868\u7ed3\u6784\u4e0d\u540c,\u6709\u4e24\u5c42\u3002\u4f46\u662f\u540c\u4e00\u6b21\u8fd0\u884c,\u54c8\u5e0c\u968f\u673a\u79cd\u5b50\u662f\u4e00\u6837\u7684,\u53ea\u8981key\u76f8\u540c,\u6700\u7ec8\u83b7\u53d6\u5230\u7684\u4e0b\u5c42\u7684\u623f\u95f4\u53f7(\u952e\u503c\u5bf9\u5217\u8868\u7d22\u5f15)\u76f8\u540c\u3002\n\u7531\u4e8ekey\u90fd\u662f\u4e0d\u53ef\u53d8\u91cf,\u53ea\u8981\u5185\u5bb9\u76f8\u540c,\u90a3\u4e48\u5185\u5b58\u5730\u5740\u4e5f\u4f1a\u76f8\u540c,\u4e0d\u540c\u5728\u4e8evalue\u7684\u5185\u5b58\u5730\u5740,\u8fd9\u5c31\u8ddf\u3002\u793a\u4f8bvalue\u90fd\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b,\u6240\u4ee5\u53ea\u8981value\u503c\u76f8\u540c,\u5185\u5b58\u5730\u5740\u4e5f\u76f8\u540c\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u780110(value\u6709\u53ef\u53d8\u7c7b\u578b):<\/p>\n<pre><code class=\"lang-python language-python python\">import copy\n\nd1 = {&quot;k1&quot;: [1, 2, 3], &quot;k2&quot;: &quot;v2&quot;, &quot;k3&quot;: &quot;v3&quot;}\nd2 = copy.copy(d1)\nd3 = copy.deepcopy(d1)\nd4 = d1\n\nprint(id(d1), id(d2), id(d3), id(d4))\nprint(f&quot;{'-' * 16}\u672a\u4fee\u6539\u6570\u636e{'-' * 16}&quot;)\nprint(&quot;d1=&quot;, d1, &quot;\\nd2=&quot;, d2, &quot;\\nd3=&quot;, d3, &quot;\\nd4=&quot;, d4)\n\nd1[&quot;k1&quot;][0] = 100\nprint(f&quot;{'-' * 16}\u4fee\u6539d1\u7684k1[0]\u6570\u636e{'-' * 16}&quot;)\nprint(id(d1), id(d2), id(d3), id(d4))\nprint(&quot;d1=&quot;, d1, &quot;\\nd2=&quot;, d2, &quot;\\nd3=&quot;, d3, &quot;\\nd4=&quot;, d4)\n\nd2[&quot;k1&quot;][1] = 200\nprint(f&quot;{'-' * 16}\u4fee\u6539d2\u7684k1[1]\u6570\u636e{'-' * 16}&quot;)\nprint(id(d1), id(d2), id(d3), id(d4))\nprint(&quot;d1=&quot;, d1, &quot;\\nd2=&quot;, d2, &quot;\\nd3=&quot;, d3, &quot;\\nd4=&quot;, d4)\n\nd3[&quot;k1&quot;][2] = 300\nprint(f&quot;{'-' * 16}\u4fee\u6539d3\u7684k1[2]\u6570\u636e{'-' * 16}&quot;)\nprint(id(d1), id(d2), id(d3), id(d4))\nprint(&quot;d1=&quot;, d1, &quot;\\nd2=&quot;, d2, &quot;\\nd3=&quot;, d3, &quot;\\nd4=&quot;, d4)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>4311192192 4310240064 4313873408 4311192192\n----------------\u672a\u4fee\u6539\u6570\u636e----------------\nd1= {'k1': [1, 2, 3], 'k2': 'v2', 'k3': 'v3'} \nd2= {'k1': [1, 2, 3], 'k2': 'v2', 'k3': 'v3'} \nd3= {'k1': [1, 2, 3], 'k2': 'v2', 'k3': 'v3'} \nd4= {'k1': [1, 2, 3], 'k2': 'v2', 'k3': 'v3'}\n----------------\u4fee\u6539d1\u7684k1[0]\u6570\u636e----------------\n4311192192 4310240064 4313873408 4311192192\nd1= {'k1': [100, 2, 3], 'k2': 'v2', 'k3': 'v3'} \nd2= {'k1': [100, 2, 3], 'k2': 'v2', 'k3': 'v3'} \nd3= {'k1': [1, 2, 3], 'k2': 'v2', 'k3': 'v3'} \nd4= {'k1': [100, 2, 3], 'k2': 'v2', 'k3': 'v3'}\n----------------\u4fee\u6539d2\u7684k1[1]\u6570\u636e----------------\n4311192192 4310240064 4313873408 4311192192\nd1= {'k1': [100, 200, 3], 'k2': 'v2', 'k3': 'v3'} \nd2= {'k1': [100, 200, 3], 'k2': 'v2', 'k3': 'v3'} \nd3= {'k1': [1, 2, 3], 'k2': 'v2', 'k3': 'v3'} \nd4= {'k1': [100, 200, 3], 'k2': 'v2', 'k3': 'v3'}\n----------------\u4fee\u6539d3\u7684k1[2]\u6570\u636e----------------\n4311192192 4310240064 4313873408 4311192192\nd1= {'k1': [100, 200, 3], 'k2': 'v2', 'k3': 'v3'} \nd2= {'k1': [100, 200, 3], 'k2': 'v2', 'k3': 'v3'} \nd3= {'k1': [1, 2, 300], 'k2': 'v2', 'k3': 'v3'} \nd4= {'k1': [100, 200, 3], 'k2': 'v2', 'k3': 'v3'}<\/code><\/pre>\n<p>\u4e0elist\u8fd0\u884c\u7ed3\u679c\u8868\u73b0\u4e00\u81f4\ndict\u603b\u7ed3:<\/p>\n<ul>\n<li>\u6d45\u62f7\u8d1d:\n<ul>\n<li>\u62f7\u8d1d\u5143\u6570\u636e,\u5b58\u653e\u72ec\u7acb\u5185\u5b58\u7a7a\u95f4<\/li>\n<li>\u62f7\u8d1d\u4e2d\u95f4\u8868,\u5b58\u653e\u72ec\u7acb\u7a7a\u95f4,\u4e2d\u95f4\u8868\u4e0d\u4fee\u6539<\/li>\n<\/ul><\/li>\n<li>\u6df1\u62f7\u8d1d:\n<ul>\n<li>\u62f7\u8d1d\u5143\u6570\u636e,\u5b58\u653e\u72ec\u7acb\u5185\u5b58\u7a7a\u95f4<\/li>\n<li>\u62f7\u8d1d\u4e2d\u95f4\u8868,\u5b58\u653e\u72ec\u7acb\u7a7a\u95f4,\u4e2d\u95f4\u8868\u5bf9value\u4e3a\u53ef\u53d8\u7c7b\u578b\u6570\u636e\u5f00\u8f9f\u65b0\u5185\u5b58\u5b58\u653e,\u5176\u4f59\u4e0d\u53d8<\/li>\n<\/ul><\/li>\n<li>\u8d4b\u503c:\n\u7ed9\u539f\u6765\u7684\u53d8\u91cf\u52a0\u4e00\u4e2a\u65b0\u540d\u79f0,\u5c31\u662f\u540c\u4e00\u4e2a<\/li>\n<\/ul>\n<hr \/>\n<h2>\u6e90\u7801<\/h2>\n<pre><code class=\"lang-python language-python python\">&quot;&quot;&quot;Generic (shallow and deep) copying operations.\n\nInterface summary:\n\n        import copy\n\n        x = copy.copy(y)        # make a shallow copy of y\n        x = copy.deepcopy(y)    # make a deep copy of y\n\nFor module specific errors, copy.Error is raised.\n\nThe difference between shallow and deep copying is only relevant for\ncompound objects (objects that contain other objects, like lists or\nclass instances).\n\n- A shallow copy constructs a new compound object and then (to the\n  extent possible) inserts *the same objects* into it that the\n  original contains.\n\n- A deep copy constructs a new compound object and then, recursively,\n  inserts *copies* into it of the objects found in the original.\n\nTwo problems often exist with deep copy operations that don't exist\nwith shallow copy operations:\n\n a) recursive objects (compound objects that, directly or indirectly,\n    contain a reference to themselves) may cause a recursive loop\n\n b) because deep copy copies *everything* it may copy too much, e.g.\n    administrative data structures that should be shared even between\n    copies\n\nPython's deep copy operation avoids these problems by:\n\n a) keeping a table of objects already copied during the current\n    copying pass\n\n b) letting user-defined classes override the copying operation or the\n    set of components copied\n\nThis version does not copy types like module, class, function, method,\nnor stack trace, stack frame, nor file, socket, window, nor any\nsimilar types.\n\nClasses can use the same interfaces to control copying that they use\nto control pickling: they can define methods called __getinitargs__(),\n__getstate__() and __setstate__().  See the documentation for module\n&quot;pickle&quot; for information on these methods.\n&quot;&quot;&quot;\n\nimport types\nimport weakref\nfrom copyreg import dispatch_table\n\nclass Error(Exception):\n    pass\nerror = Error   # backward compatibility\n\n__all__ = [&quot;Error&quot;, &quot;copy&quot;, &quot;deepcopy&quot;]\n\ndef copy(x):\n    &quot;&quot;&quot;Shallow copy operation on arbitrary Python objects.\n\n    See the module's __doc__ string for more info.\n    &quot;&quot;&quot;\n\n    cls = type(x)\n\n    copier = _copy_dispatch.get(cls)\n    if copier:\n        return copier(x)\n\n    if issubclass(cls, type):\n        # treat it as a regular class:\n        return _copy_immutable(x)\n\n    copier = getattr(cls, &quot;__copy__&quot;, None)\n    if copier is not None:\n        return copier(x)\n\n    reductor = dispatch_table.get(cls)\n    if reductor is not None:\n        rv = reductor(x)\n    else:\n        reductor = getattr(x, &quot;__reduce_ex__&quot;, None)\n        if reductor is not None:\n            rv = reductor(4)\n        else:\n            reductor = getattr(x, &quot;__reduce__&quot;, None)\n            if reductor:\n                rv = reductor()\n            else:\n                raise Error(&quot;un(shallow)copyable object of type %s&quot; % cls)\n\n    if isinstance(rv, str):\n        return x\n    return _reconstruct(x, None, *rv)\n\n_copy_dispatch = d = {}\n\ndef _copy_immutable(x):\n    return x\nfor t in (types.NoneType, int, float, bool, complex, str, tuple,\n          bytes, frozenset, type, range, slice, property,\n          types.BuiltinFunctionType, types.EllipsisType,\n          types.NotImplementedType, types.FunctionType, types.CodeType,\n          weakref.ref):\n    d[t] = _copy_immutable\n\nd[list] = list.copy\nd[dict] = dict.copy\nd[set] = set.copy\nd[bytearray] = bytearray.copy\n\ndel d, t\n\ndef deepcopy(x, memo=None, _nil=[]):\n    &quot;&quot;&quot;Deep copy operation on arbitrary Python objects.\n\n    See the module's __doc__ string for more info.\n    &quot;&quot;&quot;\n\n    if memo is None:\n        memo = {}\n\n    d = id(x)\n    y = memo.get(d, _nil)\n    if y is not _nil:\n        return y\n\n    cls = type(x)\n\n    copier = _deepcopy_dispatch.get(cls)\n    if copier is not None:\n        y = copier(x, memo)\n    else:\n        if issubclass(cls, type):\n            y = _deepcopy_atomic(x, memo)\n        else:\n            copier = getattr(x, &quot;__deepcopy__&quot;, None)\n            if copier is not None:\n                y = copier(memo)\n            else:\n                reductor = dispatch_table.get(cls)\n                if reductor:\n                    rv = reductor(x)\n                else:\n                    reductor = getattr(x, &quot;__reduce_ex__&quot;, None)\n                    if reductor is not None:\n                        rv = reductor(4)\n                    else:\n                        reductor = getattr(x, &quot;__reduce__&quot;, None)\n                        if reductor:\n                            rv = reductor()\n                        else:\n                            raise Error(\n                                &quot;un(deep)copyable object of type %s&quot; % cls)\n                if isinstance(rv, str):\n                    y = x\n                else:\n                    y = _reconstruct(x, memo, *rv)\n\n    # If is its own copy, don't memoize.\n    if y is not x:\n        memo[d] = y\n        _keep_alive(x, memo) # Make sure x lives at least as long as d\n    return y\n\n_deepcopy_dispatch = d = {}\n\ndef _deepcopy_atomic(x, memo):\n    return x\nd[types.NoneType] = _deepcopy_atomic\nd[types.EllipsisType] = _deepcopy_atomic\nd[types.NotImplementedType] = _deepcopy_atomic\nd[int] = _deepcopy_atomic\nd[float] = _deepcopy_atomic\nd[bool] = _deepcopy_atomic\nd[complex] = _deepcopy_atomic\nd[bytes] = _deepcopy_atomic\nd[str] = _deepcopy_atomic\nd[types.CodeType] = _deepcopy_atomic\nd[type] = _deepcopy_atomic\nd[range] = _deepcopy_atomic\nd[types.BuiltinFunctionType] = _deepcopy_atomic\nd[types.FunctionType] = _deepcopy_atomic\nd[weakref.ref] = _deepcopy_atomic\nd[property] = _deepcopy_atomic\n\ndef _deepcopy_list(x, memo, deepcopy=deepcopy):\n    y = []\n    memo[id(x)] = y\n    append = y.append\n    for a in x:\n        append(deepcopy(a, memo))\n    return y\nd[list] = _deepcopy_list\n\ndef _deepcopy_tuple(x, memo, deepcopy=deepcopy):\n    y = [deepcopy(a, memo) for a in x]\n    # We're not going to put the tuple in the memo, but it's still important we\n    # check for it, in case the tuple contains recursive mutable structures.\n    try:\n        return memo[id(x)]\n    except KeyError:\n        pass\n    for k, j in zip(x, y):\n        if k is not j:\n            y = tuple(y)\n            break\n    else:\n        y = x\n    return y\nd[tuple] = _deepcopy_tuple\n\ndef _deepcopy_dict(x, memo, deepcopy=deepcopy):\n    y = {}\n    memo[id(x)] = y\n    for key, value in x.items():\n        y[deepcopy(key, memo)] = deepcopy(value, memo)\n    return y\nd[dict] = _deepcopy_dict\n\ndef _deepcopy_method(x, memo): # Copy instance methods\n    return type(x)(x.__func__, deepcopy(x.__self__, memo))\nd[types.MethodType] = _deepcopy_method\n\ndel d\n\ndef _keep_alive(x, memo):\n    &quot;&quot;&quot;Keeps a reference to the object x in the memo.\n\n    Because we remember objects by their id, we have\n    to assure that possibly temporary objects are kept\n    alive by referencing them.\n    We store a reference at the id of the memo, which should\n    normally not be used unless someone tries to deepcopy\n    the memo itself...\n    &quot;&quot;&quot;\n    try:\n        memo[id(memo)].append(x)\n    except KeyError:\n        # aha, this is the first one :-)\n        memo[id(memo)]=[x]\n\ndef _reconstruct(x, memo, func, args,\n                 state=None, listiter=None, dictiter=None,\n                 *, deepcopy=deepcopy):\n    deep = memo is not None\n    if deep and args:\n        args = (deepcopy(arg, memo) for arg in args)\n    y = func(*args)\n    if deep:\n        memo[id(x)] = y\n\n    if state is not None:\n        if deep:\n            state = deepcopy(state, memo)\n        if hasattr(y, '__setstate__'):\n            y.__setstate__(state)\n        else:\n            if isinstance(state, tuple) and len(state) == 2:\n                state, slotstate = state\n            else:\n                slotstate = None\n            if state is not None:\n                y.__dict__.update(state)\n            if slotstate is not None:\n                for key, value in slotstate.items():\n                    setattr(y, key, value)\n\n    if listiter is not None:\n        if deep:\n            for item in listiter:\n                item = deepcopy(item, memo)\n                y.append(item)\n        else:\n            for item in listiter:\n                y.append(item)\n    if dictiter is not None:\n        if deep:\n            for key, value in dictiter:\n                key = deepcopy(key, memo)\n                value = deepcopy(value, memo)\n                y[key] = value\n        else:\n            for key, value in dictiter:\n                y[key] = value\n    return y\n\ndel types, weakref\n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>\u9605\u8bfb\u672c\u7ae0\u8282\u524d,\u5efa\u8bae\u5148\u67e5\u770b &#8220;\u6570\u636e\u7c7b\u578b\u4e0e\u64cd\u4f5c\u65b9\u6cd5&#8221; \u5728 Python \u4e2d\uff0c\u6d45\u62f7\u8d1d\uff08Shallow Copy\uff09\u548c\u6df1\u62f7\u8d1d\uff08Deep Copy\uff09&#8230;<\/p>","protected":false},"author":1,"featured_media":109,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[45],"class_list":["post-126","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\/126","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=126"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions\/127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/media\/109"}],"wp:attachment":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}