{"id":260287,"date":"2023-01-19T09:36:48","date_gmt":"2023-01-19T01:36:48","guid":{"rendered":"https:\/\/gulass.cn\/?p=260287"},"modified":"2022-12-27T21:37:34","modified_gmt":"2022-12-27T13:37:34","slug":"python-assignment-copy","status":"publish","type":"post","link":"https:\/\/gulass.cn\/python-assignment-copy.html","title":{"rendered":"python\u8d4b\u503c\u548c\u62f7\u8d1d"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8d4b\u503c\uff0c\u503c\u76f8\u540c\uff0c\u5185\u5b58\u5730\u5740\u76f8\u540c\u2013\u51fd\u6570\u53c2\u6570\u4f20\u9012\u3002\u6d45\u62f7\u8d1d\uff0c\u503c\u76f8\u540c\uff0c\u5185\u5b58\u5730\u5740\u4e0d\u540c\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u62f7\u8d1d\u7b2c\u4e00\u5c42\u5185\u5b58\u5730\u5740\u7684\u5f15\u7528\u3002\u7b2c\u4e00\u5c42\u5143\u7d20\u4e3a\u53ef\u53d8\u5143\u7d20\u3002\u62f7\u8d1d\u8fc7\u7684\u5f15\u7528\u4f1a\u8ddf\u7740\u53d1\u751f\u53d8\u5316\u3002\u5426\u5219\u4e0d\u53d1\u751f\u53d8\u5316<\/p>\n

\r\nimport copy\r\na = [[], 'hello', 'world', {}]\r\nb = a\r\nc = copy.copy(a)\r\na\r\n[[], 'hello', 'world', {}]\r\nb\r\n[[], 'hello', 'world', {}]\r\nc\r\n[[], 'hello', 'world', {}]\r\na[0].append('hi')\r\na.append('\u5c0f\u660e')\r\na\r\n[['hi'], 'hello', 'world', {}, '\u5c0f\u660e']\r\nb\r\n[['hi'], 'hello', 'world', {}, '\u5c0f\u660e']\r\nc\r\n[['hi'], 'hello', 'world', {}]\r\n<\/pre>\n

\u6d45\u62f7\u8d1d\u53ea\u662f\u62f7\u8d1d\u7b2c\u4e00\u5c42\u5730\u5740\u7684\u5f15\u7528\u3002<\/p>\n

\u51fd\u6570\u53c2\u6570\u4f20\u9012\u2013\u5f15\u7528<\/strong><\/div>\n
\r\nclass T:\r\n\r\n    def __init__(self, passengers=[]):\r\n        self.passengers = passengers\r\n\r\n    def pick(self, v):\r\n        self.passengers.append(v)\r\n\r\n    def remove(self, v):\r\n        self.passengers.remove(v)\r\n\r\n        \r\nt1 = T()\r\nt1..pick('hi')\r\nt2 = T()\r\nt1.passengers\r\nt2.passengers\r\n\r\n# ['hi']\r\n# ['hi']\r\n<\/pre>\n

\u51fa\u73b0\u95ee\u9898\u7684\u539f\u56e0\u662f\uff1a\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u6ca1\u6709\u6307\u5b9a passengers \u5b9e\u4f8b\u4f1a\u5171\u4eab\u5217\u8868<\/p>\n

\u95ee\u9898\u6839\u6e90\uff1a\u9ed8\u8ba4\u503c\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u8ba1\u7b97\uff08\u901a\u5e38\u5728\u52a0\u8f7d\u6a21\u5757\u65f6\uff09__init__\u68c0\u67e5\u53c2\u6570\u4e3aNone\u8d4b\u503c\u4e00\u4e2a\u5217\u8868\u3002\u5426\u5219\u793a\u4f8b\u5316\u65f6\u5171\u4eab\u5217\u8868\u3002<\/p>\n

\u9632\u5fa1\u53ef\u53d8\u51fd\u6570\u5e26\u6765\u7684\u5f71\u54cd<\/strong><\/div>\n

\u5982\u679c\u51fd\u6570\u63a5\u53d7\u53ef\u53d8\u53c2\u6570\u3002\u5e94\u8be5\u8c28\u614e\u8003\u8651\u8c03\u7528\u65b9\u6cd5\u662f\u5426\u671f\u671b\u4fee\u6539\u4f20\u5165\u7684\u53c2\u6570\u3002<\/p>\n

\u4f8b\u5982\uff1a\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u800c\u4e14\u5728\u5904\u7406\u7684\u8fc7\u7a0b\u4e2d\u9700\u8981\u4fee\u6539\u5b83\u3002\u90a3\u4e48\uff0c\u4f60\u8981\u8003\u8651\uff01\uff01\uff01\u51fd\u6570\u5916\u90e8\u662f\u5426\u4f1a\u6536\u5230\u5f71\u54cd<\/p>\n

\r\n# passengers \u907f\u514d\u53d7\u5230\u5f71\u54cd\r\n\r\nclass Test:\r\n\r\n    def __init__(self, passengers=None):\r\n        if passengers is None:\r\n            self.passengers = []\r\n        else:\r\n            # \u6df1\u62f7\u8d1d\r\n            self.passengers = list(passengers)\r\n\r\n    def pick(self, v):\r\n        self.passengers.append(v)\r\n\r\n    def remove(self, v):\r\n        self.passengers.remove(v)\r\n        \r\n        \r\n def test(passengers=None):\r\n    if passengers is None:\r\n        passengers = []\r\n    else:\r\n        # \u6df1\u62f7\u8d1d\r\n        passengers = copy.deepcopy(passengers)\r\n<\/pre>\n

\u51fd\u6570\u4f20\u9012\u7684\u65b9\u5f0f<\/p>\n

\u4eba\u4eec\u7ecf\u5e38\u8bf4\uff1a\u53c2\u6570\u6309\u503c\u4f20\u9012\uff0c\u4f46\u662f\u8fd9\u91cc\u662f\u503c\u5f97\u5f15\u7528\u3002\u8fd9\u4e48\u8bf4\u6ca1\u6709\u9519\uff0c\u4f46\u662f\u4f1a\u5f15\u8d77\u8bef\u89e3\u3002\u56e0\u4e3a\uff0c\u5728\u65e7\u5f0f\u8bed\u8a00\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\u6709\u6839\u636e\u503c\u4f20\u9012\uff08\u51fd\u6570\u5f97\u5230\u53c2\u6570\u7684\u526f\u672c\uff09\u548c\u6309\u7167\u5f15\u7528\u4f20\u9012\uff08\u51fd\u6570\u5f97\u5230\u53c2\u6570\u7684\u6307\u9488\uff09\u3002Python\u4e2d\u51fd\u6570\u5f97\u5230\u53c2\u6570\u7684\u526f\u672c\uff08\u53ef\u4ee5\u7406\u89e3\u4e3a\u6d45\u62f7\u8d1d\uff09\uff0c\u4f46\u662f\u53c2\u6570\u59cb\u7ec8\u662f\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u53d8\u5bf9\u8c61\uff0c\u90a3\u4e48\u5bf9\u8c61\u53ef\u80fd\u88ab\u4fee\u6539\uff0c\u4f46\u662f\u5bf9\u8c61\u7684\u6807\u8bc6\uff08\u5185\u5b58\u5730\u5740\uff09\u4e0d\u53d8\u3002\u6b64\u5916\uff0c\u56e0\u4e3a\u51fd\u6570\u5f97\u5230\u7684\u53c2\u6570\u662f\u5f15\u7528\u7684\u526f\u672c\u3002\u6240\u4ee5\u91cd\u65b0\u7ed1\u5b9a\u5bf9\u51fd\u6570\u7684\u5916\u90e8\u6ca1\u6709\u5f71\u54cd\u3002<\/p>\n

\u7b80\u5355\u7406\u89e3<\/strong><\/div>\n
\r\ndef func(x=[[], 1]):\r\n    print(x)\r\n    x.append('a')\r\n    print(x)\r\n    x[0].append('b')\r\n    print(x)\r\n# func \u53c2\u6570\u65f6\u4e00\u4e2a\u53ef\u53d8\u7c7b\u578b\r\nfunc()\r\nfunc()\r\n# \u7b2c\u4e00\u6b21\u8c03\u7528x=[['b'], 1, 'a']\r\n# \u6b64\u65f6\u5728func(x=[['b'], 1, 'a']) \u4e0d\u5728\u662f func(x=[[], 1])\u3002\u6709\u70b9\u7c7b\u4f3c \u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684 x=[[], 1]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u62f7\u8d1d\u7b2c\u4e00\u5c42\u5185\u5b58\u5730\u5740\u7684\u5f15\u7528\u3002\u7b2c\u4e00\u5c42\u5143\u7d20\u4e3a\u53ef\u53d8\u5143\u7d20\u3002\u62f7\u8d1d\u8fc7\u7684\u5f15\u7528\u4f1a\u8ddf\u7740\u53d1\u751f\u53d8\u5316\u3002\u5426\u5219\u4e0d\u53d1\u751f\u53d8\u5316 import co […]<\/p>\n","protected":false},"author":1903,"featured_media":260311,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-260287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/260287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=260287"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/260287\/revisions"}],"predecessor-version":[{"id":260314,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/260287\/revisions\/260314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/260311"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=260287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=260287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=260287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}