{"id":244,"date":"2016-07-10T17:54:48","date_gmt":"2016-07-10T09:54:48","guid":{"rendered":"https:\/\/vinta.ws\/code\/?p=244"},"modified":"2026-02-18T01:20:36","modified_gmt":"2026-02-17T17:20:36","slug":"python-is-call-by-assignment","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/python-is-call-by-assignment.html","title":{"rendered":"Python is call-by-assignment"},"content":{"rendered":"<p>If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart's delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you're done, the outer reference will still point at the original object.<\/p>\n<p>If you pass an immutable object to a method, you still can't rebind the outer reference, and you can't even mutate the object.<\/p>\n<p>\u4f8b\u5982 <code>def foo(bar):<\/code> \u9019\u500b function \u7684\u53c3\u6578\u50b3\u905e\uff0c\u5176\u5be6\u662f\u5728 <code>foo<\/code> \u7684 local namespace \u88e1\u5e6b\u50b3\u9032\u53bb\u7684\u7269\u4ef6\u7d81\u5b9a\u4e86\u4e00\u500b\u53eb\u505a <code>bar<\/code> \u7684\u540d\u5b57\uff08\u6240\u8b02\u7684 assignment\uff09\uff1b\u5982\u679c\u4f60\u5728 <code>foo<\/code> \u88e1 re-assign \u4e86\u4e00\u500b\u65b0\u7684\u7269\u4ef6\u7d66 <code>bar<\/code>\uff0c\u5be6\u969b\u4e0a\u662f\u628a <code>bar<\/code> \u9019\u500b\u540d\u5b57\u7d81\u5b9a\u5230\u90a3\u500b\u65b0\u7684\u7269\u4ef6\u3002<\/p>\n<ul>\n<li>mutable objects: list, dict, set \u7684\u884c\u70ba\u540c call-by-reference<\/li>\n<li>immutable objects: boolean, int, float, long, str, unicode, tuple \u7684\u884c\u70ba\u540c call-by-value<\/li>\n<\/ul>\n<p>\u4e0d\u904e\u5982\u679c\u4f60\u628a\u4e00\u500b mutable \u7269\u4ef6\u653e\u9032 immutable \u7269\u4ef6\u88e1\uff0c\u4f8b\u5982\u628a list \u653e\u9032 tuple \u88e1\uff0c\u5247\u4fee\u6539\u4e86 list \u4e4b\u5f8c\uff0c\u90a3\u500b tuple \u88e1\u7684 list \u4e5f\u662f\u6703\u88ab\u4fee\u6539\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">a_list = [1, 2, 3]\na_tuple = (a_list, 'a', 'b', 'c')\na_list.append(4)<\/code><\/pre>\n<p>Assignment is the binding of a name to an object: <code>name = &#039;Molly&#039;<\/code>.<br \/>\nAssignment between names doesn't create a new object, both names are simply bound to the same object: <code>nickname = name<\/code>.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">name = 'Mollie'\nname = 'Vinta'<\/code><\/pre>\n<p>\u6240\u8b02\u7684 assign \u9019\u500b\u52d5\u4f5c\uff0c\u5176\u5be6\u662f\u5e6b 'Mollie' \u9019\u500b\u5b57\u4e32\u53d6\u4e00\u500b\u540d\u5b57\u53eb\u505a name\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u53c8\u52a0\u4e0a\u4e00\u53e5 <code>name = &#039;Vinta&#039;<\/code>\uff0c\u5be6\u969b\u4e0a\u662f\u5efa\u7acb\u4e86\u4e00\u500b\u65b0\u7684\u7269\u4ef6\uff08\u5b57\u4e32 'Vinta'\uff09\uff0c\u518d\u628a\u9019\u500b\u65b0\u5b57\u4e32\u7d81\u5b9a\u5230 name \u9019\u500b\u540d\u5b57\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\"># if bar refers to a mutable object\ndef foo(bar):\n    bar.append('new value')\n    print(bar)\n    # output: ['old value', 'new value']\n\nanswer_list = ['old value', ]\nfoo(answer_list)\nprint(answer_list)\n# output: ['old value', 'new value']\n\n# if bar refers to an immutable object\ndef foo(bar):\n    bar = 'new value'\n    print(bar)\n    # output: 'new value'\n\nanswer_list = 'old value'\nfoo(answer_list)\nprint(answer_list)\n# output: 'old value'\n\n# if bar refers to a mutable object and re-assign it in foo\ndef foo(bar):\n    bar = ['new value', ]\n    print(bar)\n    # output: ['new value', ]\n\nanswer_list = ['old value', ]\nfoo(answer_list)\nprint(answer_list)\n# output: ['old value', ]<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/986006\/how-do-i-pass-a-variable-by-reference\">https:\/\/stackoverflow.com\/questions\/986006\/how-do-i-pass-a-variable-by-reference<\/a><br \/>\n<a href=\"https:\/\/jeffknupp.com\/blog\/2012\/11\/13\/is-python-callbyvalue-or-callbyreference-neither\/\">https:\/\/jeffknupp.com\/blog\/2012\/11\/13\/is-python-callbyvalue-or-callbyreference-neither\/<\/a><br \/>\n<a href=\"https:\/\/docs.python.org\/3\/faq\/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference\">https:\/\/docs.python.org\/3\/faq\/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference<\/a><\/p>\n<h2>scope<\/h2>\n<p>\u53ea\u6709 module\u3001class\u3001function \u624d\u6709\u5efa\u7acb\u65b0\u7684 scope\uff0cif\u3001while\u3001for \u4e26\u4e0d\u6703\u5efa\u7acb\u65b0\u7684 scope\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">some_list = [\n    {\n        'id': 1,\n        'is_change': False,\n    },\n    {\n        'id': 2,\n        'is_change': False,\n    },\n    {\n        'id': 3,\n        'is_change': False,\n    },\n    {\n        'id': 4,\n        'is_change': False,\n    },\n]\n\nfor item in some_list:\n    item['is_change'] = True<\/code><\/pre>\n<p><code>some_list<\/code> \u7684\u6bcf\u500b item \u90fd\u6703\u88ab\u66f4\u65b0\uff0c\u56e0\u70ba\u662f mutable objects \u7684\u884c\u70ba\u662f call-by-reference\u3002\u6240\u4ee5\u4f60\u4e0d\u9700\u8981\u9019\u6a23\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">new_list = []\nfor item in some_list:\n    item['is_change'] = True\n    new_list.append(item)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart's delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you're done, the outer reference will still point at the original object.<\/p>\n<p>If you pass an immutable object to a method, you still can't rebind the outer reference, and you can't even mutate the object.<\/p>\n","protected":false},"author":1,"featured_media":761,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[2],"class_list":["post-244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-python","tag-python"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/761"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}