﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-糯米-随笔分类-Python</title><link>http://www.cppblog.com/varg-vikernes/category/16061.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 24 Feb 2011 07:15:13 GMT</lastBuildDate><pubDate>Thu, 24 Feb 2011 07:15:13 GMT</pubDate><ttl>60</ttl><item><title>python中最容易让人火大的两个问题</title><link>http://www.cppblog.com/varg-vikernes/archive/2011/02/08/139803.html</link><dc:creator>糯米</dc:creator><author>糯米</author><pubDate>Tue, 08 Feb 2011 07:38:00 GMT</pubDate><guid>http://www.cppblog.com/varg-vikernes/archive/2011/02/08/139803.html</guid><wfw:comment>http://www.cppblog.com/varg-vikernes/comments/139803.html</wfw:comment><comments>http://www.cppblog.com/varg-vikernes/archive/2011/02/08/139803.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/varg-vikernes/comments/commentRss/139803.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/varg-vikernes/services/trackbacks/139803.html</trackback:ping><description><![CDATA[1. list对象的*操作符<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;a&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[[</span><span style="color: #000000;">1</span><span style="color: #000000;">]]</span><span style="color: #000000;">*</span><span style="color: #000000;">10</span><span style="color: #000000;"><br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;a<br>[[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">1</span><span style="color: #000000;">]]<br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;a[</span><span style="color: #000000;">1</span><span style="color: #000000;">][0]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;"><br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;a<br>[[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">]]<br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;"> <br></span></div>
也就是说，这10个对象实际上是指向的同一个list对象。<br>这是bug，还是feature？或者是优化？<br>总之是蛮让人火大的就是了。<br>用 a = [[0] for x in range(10)] 这种写法就没有这个问题了。<br><br><br>2. 深拷贝<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;a&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[[0]&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;range(</span><span style="color: #000000;">10</span><span style="color: #000000;">)]<br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;a<br>[[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0]]<br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;b&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;list(a)<br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;b<br>[[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0]]<br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;a[</span><span style="color: #000000;">1</span><span style="color: #000000;">][0]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;"><br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;b<br>[[0],&nbsp;[</span><span style="color: #000000;">2</span><span style="color: #000000;">],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0],&nbsp;[0]]<br></span><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #000000;">&nbsp;<br></span></div>
b = list(a) <br>意味着a和b中都存放这10个指针。指向[0], [0], [0] .... 这10个对象。<br>a[1][0] = 2 后 a 自己的值没有改变，改变的是第二个 [0] 对象。<br>由于 b 也是指向它的，所以打印b的时候会发现这一点。<br>这个问题是自己经常犯的问题，大多都是debug半天才知道怎么回事。<br>使用<br>import copy<br>b = copy.deepcopy(a)<br>可以解决这个问题。<br><br>3. 如何避免这些问题<br>要时刻记得，python中的对象就只有两种，mutable和immutable。也就是可改变和不可改变。<br>immutable的包括：str&nbsp; tuple&nbsp; int ...<br>mutable可改变的包括：list dict ...<br>immutable的就是原子的。mutable里面存放的都是指向mutable或者immutable的指针。<br>调试的时候，可以使用id(obj)获得每个对象的id。这个貌似就是python管理的运行时的对象的地址。<br>如果发现两个obj的id相同，那他们就是同一个货。。<br> <img src ="http://www.cppblog.com/varg-vikernes/aggbug/139803.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/varg-vikernes/" target="_blank">糯米</a> 2011-02-08 15:38 <a href="http://www.cppblog.com/varg-vikernes/archive/2011/02/08/139803.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>