{"id":2397,"date":"2015-07-26T10:18:59","date_gmt":"2015-07-26T02:18:59","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2397"},"modified":"2015-07-26T10:18:59","modified_gmt":"2015-07-26T02:18:59","slug":"python%e4%b8%ad%e7%9a%84%e5%8f%98%e9%87%8f%e5%bc%95%e7%94%a8%e6%8b%b7%e8%b4%9d","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2397.html","title":{"rendered":"Python\u4e2d\u7684\u53d8\u91cf\/\u5f15\u7528\/\u62f7\u8d1d"},"content":{"rendered":"<h5>=Python\u4e2d\u7684\u53d8\u91cf\u548c\u5f15\u7528=<\/h5>\n<p>\u5728 Python \u4e2d\u8d4b\u503c\u8bed\u53e5\u603b\u662f\u5efa\u7acb\u5bf9\u8c61\u7684\u5f15\u7528\u503c\uff0c\u800c\u4e0d\u662f\u590d\u5236\u5bf9\u8c61\u3002\u56e0\u6b64\uff0cPython \u53d8\u91cf\u66f4\u50cf\u662f\u6307\u9488\uff0c\u800c\u4e0d\u662f\u6570\u636e\u5b58\u50a8\u533a\u57df\u3002<\/p>\n<p><a href=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2015\/07\/py_var_assignment.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2398\" src=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2015\/07\/py_var_assignment.jpg\" alt=\"py_var_assignment\" width=\"668\" height=\"316\" \/><\/a><\/p>\n<p>\u5728Python\u4e2d\uff0c\u5bf9\u8c61\u5206\u4e3a\u4e24\u79cd\uff1a<span style=\"color: #ff0000;\"><strong>\u53ef\u53d8\u5bf9\u8c61<\/strong><\/span>\u548c<span style=\"color: #ff0000;\"><strong>\u4e0d\u53ef\u53d8\u5bf9\u8c61<\/strong><\/span>\uff0c\u4e0d\u53ef\u53d8\u5bf9\u8c61\u5305\u62ecint\uff0cfloat\uff0clong\uff0cstr\uff0ctuple\u7b49\uff0c\u53ef\u53d8\u5bf9\u8c61\u5305\u62eclist\uff0cset\uff0cdict\u7b49\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u8fd9\u91cc\u8bf4\u7684\u4e0d\u53ef\u53d8\u6307\u7684\u662f\u503c\u7684\u4e0d\u53ef\u53d8\u3002\u5bf9\u4e8e\u4e0d\u53ef\u53d8\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u5982\u679c\u8981\u66f4\u6539\u53d8\u91cf\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u503c\uff0c\u628a\u53d8\u91cf\u7ed1\u5b9a\u5230\u65b0\u503c\u4e0a\uff0c\u800c\u65e7\u503c\u5982\u679c\u6ca1\u6709\u88ab\u5f15\u7528\u5c31\u7b49\u5f85\u5783\u573e\u56de\u6536\u3002\u53e6\u5916\uff0c\u4e0d\u53ef\u53d8\u7684\u7c7b\u578b\u53ef\u4ee5\u8ba1\u7b97hash\u503c\uff0c\u4f5c\u4e3a\u5b57\u5178\u7684key\u3002\u53ef\u53d8\u7c7b\u578b\u6570\u636e\u5bf9\u5bf9\u8c61\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u4e0d\u9700\u8981\u518d\u5728\u5176\u4ed6\u5730\u65b9\u7533\u8bf7\u5185\u5b58\uff0c\u53ea\u9700\u8981\u5728\u6b64\u5bf9\u8c61\u540e\u9762\u8fde\u7eed\u7533\u8bf7(+\/-)\u5373\u53ef\uff0c\u4e5f\u5c31\u662f\u5b83\u7684\u5185\u5b58\u5730\u5740\u4f1a\u4fdd\u6301\u4e0d\u53d8\uff0c\u4f46\u533a\u57df\u4f1a\u53d8\u957f\u6216\u8005\u53d8\u77ed\u3002<\/p>\n<pre class=\"lang:default decode:true \">In [1]: a = 'ixyzero.com'\n\nIn [2]: id(a)\nOut[2]: 53442592\n\nIn [3]: a = 'aol.com'\n\nIn [4]: id(a)\nOut[4]: 53443904\n\n# \u91cd\u65b0\u8d4b\u503c\u4e4b\u540e\uff0c\u53d8\u91cfa\u7684\u5185\u5b58\u5730\u5740\u5df2\u7ecf\u53d8\u4e86\n# 'ixyzero.com'\u662fstr\u7c7b\u578b\uff0c\u4e0d\u53ef\u53d8\uff0c\u6240\u4ee5\u8d4b\u503c\u64cd\u4f5c\u77e5\u8bc6\u91cd\u65b0\u521b\u5efa\u4e86str\u5bf9\u8c61'aol.com'\uff0c\u7136\u540e\u5c06\u53d8\u91cfa\u6307\u5411\u4e86\u5b83\n\nIn [5]: aList = [1, 2, 3]\n\nIn [6]: id(aList)\nOut[6]: 53941992\n\nIn [7]: aList.append(4)\n\nIn [8]: id(aList)\nOut[8]: 53941992\n\n# list\u91cd\u65b0\u8d4b\u503c\u4e4b\u540e\uff0c\u53d8\u91cf aList \u7684\u5185\u5b58\u5730\u5740\u5e76\u672a\u6539\u53d8\n# [1, 2, 3]\u662f\u53ef\u53d8\u7684\uff0cappend\u64cd\u4f5c\u53ea\u662f\u6539\u53d8\u4e86\u5176value\uff0c\u53d8\u91cf aList \u6307\u5411\u6ca1\u6709\u53d8<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li data-wpview-marker=\"http%3A%2F%2Fxianglong.me%2Farticle%2Fpython-variable-quote-copy-and-scope%2Fhttp%3A%2F%2Fmy.oschina.net%2Fleejun2005%2Fblog%2F145911http%3A%2F%2Fblog.upsuper.org%2Fpython-variable-type-and-reference.html\"><a href=\"http:\/\/xianglong.me\/article\/python-variable-quote-copy-and-scope\/\" target=\"_blank\">http:\/\/xianglong.me\/article\/python-variable-quote-copy-and-scope\/<\/a><\/li>\n<li data-wpview-marker=\"http%3A%2F%2Fxianglong.me%2Farticle%2Fpython-variable-quote-copy-and-scope%2Fhttp%3A%2F%2Fmy.oschina.net%2Fleejun2005%2Fblog%2F145911http%3A%2F%2Fblog.upsuper.org%2Fpython-variable-type-and-reference.html\"><a href=\"http:\/\/my.oschina.net\/leejun2005\/blog\/145911\" target=\"_blank\">http:\/\/my.oschina.net\/leejun2005\/blog\/145911<\/a><\/li>\n<li data-wpview-marker=\"http%3A%2F%2Fxianglong.me%2Farticle%2Fpython-variable-quote-copy-and-scope%2Fhttp%3A%2F%2Fmy.oschina.net%2Fleejun2005%2Fblog%2F145911http%3A%2F%2Fblog.upsuper.org%2Fpython-variable-type-and-reference.html\"><a href=\"http:\/\/blog.upsuper.org\/python-variable-type-and-reference.html\" target=\"_blank\">http:\/\/blog.upsuper.org\/python-variable-type-and-reference.html<\/a><\/li>\n<\/ul>\n<h6>Python\u4e2d\u51fd\u6570\u53c2\u6570\u7684\u4f20\u9012\u65b9\u5f0f\uff0c\u503c\u4f20\u9012 or \u5f15\u7528\u4f20\u9012\uff1f<\/h6>\n<p>\u7b80\u8981\u7684\u8bf4\uff1a\u90fd\u4e0d\u662f\uff08\u548c\u4f20\u7edf\u7684C\/C++\u4e2d\u7684\u503c\u4f20\u9012\u3001\u5f15\u7528\u4f20\u9012\u90fd\u4e0d\u4e00\u6837\uff09\uff0c\u662f&#8221;call-by-object&#8221;\u3002<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>python How are arguments passed<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/986006\/how-do-i-pass-a-variable-by-reference\">http:\/\/stackoverflow.com\/questions\/986006\/how-do-i-pass-a-variable-by-reference<\/a><\/li>\n<li><a href=\"http:\/\/robertheaton.com\/2014\/02\/09\/pythons-pass-by-object-reference-as-explained-by-philip-k-dick\/\">http:\/\/robertheaton.com\/2014\/02\/09\/pythons-pass-by-object-reference-as-explained-by-philip-k-dick\/<\/a><\/li>\n<li><a href=\"https:\/\/www.jeffknupp.com\/blog\/2012\/11\/13\/is-python-callbyvalue-or-callbyreference-neither\/\">https:\/\/www.jeffknupp.com\/blog\/2012\/11\/13\/is-python-callbyvalue-or-callbyreference-neither\/<\/a><\/li>\n<\/ul>\n<h6>Python\u4e2d\u53d8\u91cf\u7684\u4f5c\u7528\u57df<\/h6>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>\u53d8\u91cf\u5206\u4e3a\u4e09\u4e2a\u4f5c\u7528\u57df\u8fdb\u884c\u67e5\u627e\uff1a\u9996\u5148\u662f\u672c\u5730\uff08Local\uff09\uff0c\u7136\u540e\u662f\u51fd\u6570\u5185\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\uff08Enclosing\uff09\uff0c\u4e4b\u540e\u662f\u5168\u5c40\uff08Global\uff09\uff0c\u6700\u540e\u662f\u5185\u7f6e\uff08Built-in\uff09\u3002\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53d8\u91cf\u540d\u8d4b\u503c\u4f1a\u521b\u5efa\u6216\u8005\u6539\u53d8\u672c\u5730\u53d8\u91cf\u3002\u5168\u5c40\u58f0\u660e\u5c06\u4f1a\u7ed9\u6620\u5c04\u5230\u6a21\u5757\u6587\u4ef6\u5185\u90e8\u7684\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u540d\u8d4b\u503c\u3002Python \u7684\u53d8\u91cf\u540d\u89e3\u6790\u673a\u5236\u4e5f\u79f0\u4e3a LEGB \u6cd5\u5219\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n<p>\u5f53\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u672a\u786e\u5b9a\u7684\u53d8\u91cf\u540d\u65f6\uff0cPython\u641c\u7d224\u4e2a\u4f5c\u7528\u57df\uff1a\u672c\u5730\u4f5c\u7528\u57df\uff08L\uff09\uff0c\u4e4b\u540e\u662f\u4e0a\u4e00\u5c42\u5d4c\u5957\u7ed3\u6784\u4e2d def \u6216 lambda \u7684\u672c\u5730\u4f5c\u7528\u57df\uff08E\uff09\uff0c\u4e4b\u540e\u662f\u5168\u5c40\u4f5c\u7528\u57df\uff08G\uff09\uff0c\u6700\u540e\u662f\u5185\u7f6e\u4f5c\u7528\u57df\uff08B\uff09\u3002\u6309\u8fd9\u4e2a\u67e5\u627e\u539f\u5219\uff0c\u5728\u7b2c\u4e00\u5904\u627e\u5230\u7684\u5730\u65b9\u505c\u6b62\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0cPython \u4f1a\u62a5\u9519\u3002<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/blog.mozilla.org\/webdev\/2011\/01\/31\/python-scoping-understanding-legb\/\" target=\"_blank\">https:\/\/blog.mozilla.org\/webdev\/2011\/01\/31\/python-scoping-understanding-legb\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/faq\/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value\" target=\"_blank\">https:\/\/docs.python.org\/2\/faq\/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value<\/a><\/li>\n<li><a href=\"http:\/\/www.toptal.com\/python\/top-10-mistakes-that-python-programmers-make\" target=\"_blank\">http:\/\/www.toptal.com\/python\/top-10-mistakes-that-python-programmers-make<\/a><\/li>\n<li><a href=\"http:\/\/blog.jobbole.com\/68256\/\" target=\"_blank\">http:\/\/blog.jobbole.com\/68256\/<\/a><\/li>\n<\/ul>\n<h5>=\u6d45\u62f7\u8d1d&amp;\u6df1\u62f7\u8d1d=<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>python list copy a[:]<\/li>\n<li>python list copy list slice<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/xianglong.me\/article\/python-variable-quote-copy-and-scope\/\">http:\/\/xianglong.me\/article\/python-variable-quote-copy-and-scope\/<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/184643\/what-is-the-best-way-to-copy-a-list\">http:\/\/stackoverflow.com\/questions\/184643\/what-is-the-best-way-to-copy-a-list<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/2612802\/how-to-clone-or-copy-a-list-in-python\">http:\/\/stackoverflow.com\/questions\/2612802\/how-to-clone-or-copy-a-list-in-python<\/a><\/li>\n<li><a href=\"http:\/\/my.oschina.net\/leejun2005\/blog\/145911\">http:\/\/my.oschina.net\/leejun2005\/blog\/145911<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">\"\"\"\nimport copy\na = [[1, 2, 3], [4, 5, 6]]\nb = a\nc = a[:]    # slicing (shallow copy)\nd = copy.copy(a)    # (shallow copy)\ne = copy.deepcopy(a)\n\nprint id(a), id(b), id(c), id(d), id(e)\nb is a\nc is a\nd is a\ne is a\n\na.append(15)\na[1][2] = 10\n\nprint a\nprint b\nprint c\nprint d\nprint e\n\"\"\"\nIn [24]: print id(a), id(b), id(c), id(d), id(e)\n139922530241208 139922530241208 139922546974448 139922529937024 139922546974376\n\nIn [25]: b is a\nOut[25]: True\n\nIn [26]: c is a\nOut[26]: False\n\nIn [27]: d is a\nOut[27]: False\n\nIn [28]: e is a\nOut[28]: False\n\nIn [29]: a.append(15)\n\nIn [30]: a\nOut[30]: [[1, 2, 3], [4, 5, 6], 15]\n\nIn [31]: b\nOut[31]: [[1, 2, 3], [4, 5, 6], 15]\n\nIn [32]: c\nOut[32]: [[1, 2, 3], [4, 5, 6]]\n\nIn [33]: d\nOut[33]: [[1, 2, 3], [4, 5, 6]]\n\nIn [34]: e\nOut[34]: [[1, 2, 3], [4, 5, 6]]\n\nIn [35]: a[1][2] = 111\n\nIn [36]: a\nOut[36]: [[1, 2, 3], [4, 5, 111], 15]\n\nIn [37]: b\nOut[37]: [[1, 2, 3], [4, 5, 111], 15]\n\nIn [38]: c\nOut[38]: [[1, 2, 3], [4, 5, 111]]\n\nIn [39]: d\nOut[39]: [[1, 2, 3], [4, 5, 111]]\n\nIn [40]: e\nOut[40]: [[1, 2, 3], [4, 5, 6]]<\/pre>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Python\u4e2d\u7684\u53d8\u91cf\u548c\u5f15\u7528= \u5728 Python \u4e2d\u8d4b\u503c\u8bed\u53e5\u603b\u662f\u5efa\u7acb\u5bf9\u8c61\u7684\u5f15\u7528\u503c\uff0c\u800c\u4e0d\u662f\u590d\u5236\u5bf9\u8c61\u3002\u56e0\u6b64\uff0cPyt [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[8],"class_list":["post-2397","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-python"],"views":2337,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2397"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2397\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}