{"id":6,"date":"2026-11-08T13:15:19","date_gmt":"2026-11-08T13:15:19","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=6"},"modified":"2026-11-08T13:15:19","modified_gmt":"2026-11-08T13:15:19","slug":"python-%e5%8f%98%e9%87%8f%e3%80%81%e7%b1%bb%e5%9e%8b%e4%b8%8e%e5%8f%af%e5%8f%98%e4%b8%8d%e5%8f%af%e5%8f%98%ef%bc%9a%e6%96%b0%e6%89%8b%e6%9c%80%e5%ae%b9%e6%98%93%e8%b8%a9%e7%9a%84%e5%9d%91","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=6","title":{"rendered":"Python \u53d8\u91cf\u3001\u7c7b\u578b\u4e0e\u53ef\u53d8\u4e0d\u53ef\u53d8\uff1a\u65b0\u624b\u6700\u5bb9\u6613\u8e29\u7684\u5751"},"content":{"rendered":"<p>\u5b66 Python \u7684\u524d\u4e24\u5468\uff0c\u5f88\u591a\u4eba\u90fd\u4f1a\u88ab\u300c\u53d8\u91cf\u5230\u5e95\u5b58\u7684\u662f\u4ec0\u4e48\u300d\u7ed5\u6655\u3002\u641e\u6e05\u695a<strong>\u5bf9\u8c61\u3001\u5f15\u7528\u3001\u53ef\u53d8\/\u4e0d\u53ef\u53d8<\/strong>\uff0c\u540e\u9762\u5199\u51fd\u6570\u3001\u6539\u5217\u8868\u3001\u4f20\u53c2\u65f6\u4f1a\u5c11\u8e29\u4e00\u5927\u534a\u5751\u3002<\/p>\n<h2>\u53d8\u91cf\u662f\u6807\u7b7e\uff0c\u4e0d\u662f\u76d2\u5b50<\/h2>\n<p>\u5728 Python \u91cc\uff0c\u53d8\u91cf\u540d\u66f4\u50cf\u8d34\u5728\u5bf9\u8c61\u4e0a\u7684<strong>\u6807\u7b7e<\/strong>\uff0c\u800c\u4e0d\u662f\u88c5\u6570\u636e\u7684\u76d2\u5b50\uff1a<\/p>\n<pre><code>a = [1, 2, 3]\nb = a\nb.append(4)\nprint(a)  # [1, 2, 3, 4]\n<\/code><\/pre>\n<p><code>b = a<\/code> \u5e76\u6ca1\u6709\u590d\u5236\u5217\u8868\uff0c\u53ea\u662f\u8ba9 <code>b<\/code> \u4e5f\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u6539 <code>b<\/code> \u7b49\u4e8e\u6539 <code>a<\/code>\u3002<\/p>\n<h2>\u4e0d\u53ef\u53d8 vs \u53ef\u53d8<\/h2>\n<ul>\n<li><strong>\u4e0d\u53ef\u53d8<\/strong>\uff1aint\u3001float\u3001str\u3001tuple\u3001frozenset\u3002\u6539\u503c\u672c\u8d28\u662f\u65b0\u5efa\u5bf9\u8c61\u518d\u6539\u6807\u7b7e\u6307\u5411\u3002<\/li>\n<li><strong>\u53ef\u53d8<\/strong>\uff1alist\u3001dict\u3001set\u3001\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\u53ef\u4ee5\u5728\u539f\u5730\u4fee\u6539\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<pre><code>x = \"hi\"\nprint(id(x))\nx = x + \"!\"\nprint(id(x))  # \u5730\u5740\u53d8\u4e86\uff0c\u8bf4\u660e\u662f\u65b0\u5b57\u7b26\u4e32\n\nnums = [1, 2]\nprint(id(nums))\nnums.append(3)\nprint(id(nums))  # \u5730\u5740\u4e0d\u53d8\uff0c\u539f\u5730\u4fee\u6539\n<\/code><\/pre>\n<h2>\u51fd\u6570\u53c2\u6570\u4e3a\u4ec0\u4e48\u300c\u6539\u4e86\u53c8\u6ca1\u6539\u300d<\/h2>\n<pre><code>def bad(items=[]):  # \u5371\u9669\uff1a\u9ed8\u8ba4\u53c2\u6570\u53ea\u521b\u5efa\u4e00\u6b21\n    items.append(1)\n    return items\n\nprint(bad())  # [1]\nprint(bad())  # [1, 1]  \u4e0a\u4e00\u6b21\u7684\u5217\u8868\u8fd8\u5728\n\ndef good(items=None):\n    if items is None:\n        items = []\n    items.append(1)\n    return items\n<\/code><\/pre>\n<p>\u53ef\u53d8\u5bf9\u8c61\u4f5c\u9ed8\u8ba4\u53c2\u6570\uff0c\u662f\u7ecf\u5178\u5751\u3002\u9ed8\u8ba4\u5199\u6210 <code>None<\/code>\uff0c\u51fd\u6570\u91cc\u518d\u65b0\u5efa\u3002<\/p>\n<h2>\u60f3\u590d\u5236\u600e\u4e48\u529e<\/h2>\n<pre><code>import copy\na = [[1], [2]]\nshallow = a.copy()      # \u6d45\u62f7\u8d1d\uff1a\u5916\u5c42\u65b0\u5217\u8868\uff0c\u5185\u5c42\u8fd8\u662f\u540c\u4e00\u6279\u5bf9\u8c61\ndeep = copy.deepcopy(a) # \u6df1\u62f7\u8d1d\uff1a\u6574\u68f5\u5bf9\u8c61\u6811\u90fd\u590d\u5236\n<\/code><\/pre>\n<h2>\u5c0f\u7ed3<\/h2>\n<ol>\n<li>\u8d4b\u503c\u662f\u8d34\u6807\u7b7e\uff0c\u4e0d\u662f\u590d\u5236\u5185\u5bb9\u3002<\/li>\n<li>\u53ef\u53d8\u5bf9\u8c61\u5171\u4eab\u5f15\u7528\u65f6\uff0c\u4e00\u8fb9\u6539\u4e24\u8fb9\u90fd\u53d8\u3002<\/li>\n<li>\u9ed8\u8ba4\u53c2\u6570\u3001\u51fd\u6570\u8fd4\u56de\u503c\u91cc\u5c3d\u91cf\u522b\u300c\u5077\u5077\u300d\u5171\u4eab\u53ef\u53d8\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66 Python \u7684\u524d\u4e24\u5468\uff0c\u5f88\u591a\u4eba\u90fd\u4f1a\u88ab\u300c\u53d8\u91cf\u5230\u5e95\u5b58\u7684\u662f\u4ec0\u4e48\u300d\u7ed5\u6655\u3002\u641e\u6e05\u695a\u5bf9\u8c61\u3001\u5f15\u7528\u3001\u53ef\u53d8\/\u4e0d\u53ef\u53d8\uff0c\u540e\u9762\u5199\u51fd\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-python-basics"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}