📄 38.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -> References and Copies</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> > <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> > <a href="33.html" class="navtitle">3. Types and Objects</a> > <span class="nonavtitle">References and Copies</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="37.html" title="Reference Counting and Garbage Collection"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=38" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="38.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="39.html" title="Built-in Types"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A54%3A35+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104028131051005091197004</font><a href="read5.asp?bookname=0735710910&snode=38&now=5%2F28%2F2002+8%3A54%3A35+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>References and Copies</h3>
<p>When a program makes an assignment such as <tT CLAss="monofont">a = b</tt>, a new reference to <TT CLass="monofont">b</tT> is created. For immutable objects such as numbers and strings, this assignment effectively creates a copy of <TT Class="monofont">b</tt>. However, the behavior is quite different for mutable objects such as lists and dictionaries. For example:</p>
<pre>
b = [1,2,3,4]
a = b # a is a reference to b
a[2] = -100 # Change an element in 'a'
print b # Produces '[1, 2, -100, 4]' </pre>
<p>Because <tt clasS="monofont">a</tt> and <Tt clAss="monofont">b</tt> refer to the same object in this example, a change made to one of the variables is reflected in the other. To avoid this, you have to create a copy of an object rather than a new reference.</P>
<p>Two types of copy operations are applied to container objects such as lists and dictionaries: a shallow copy and a deep copy. A <i>shallow copy</i> creates a new object, but populates it with references to the items contained in the original object. For example:</P>
<PRE>
b = [ 1, 2, [3,4] ]
a = b[:] # Create a shallow copy of b.
a.append(100) # Append element to a.
print b # Produces '[1,2, [3,4]]'. b unchanged.
a[2][0] = -100 # Modify an element of a.
print b # Produces '[1,2, [-100,4]]'. </pre>
<p>In this case, <TT CLass="monofont">a</tT> and <TT Class="monofont">b</TT> are separate list objects, but the elements they contain are shared. Thus, a modification to one of the elements of <TT class="monofont">a</tt> also modifies an element of <tt class="monofont">b</tt> as shown.</p>
<p>A <i>deep copy</i> creates a new object and recursively copies all the objects it contains. There is no built-in function to create deep copies of objects. However, the <Tt cLass="monofont">copy.deepcopy()</Tt> function in the standard library can be used, as shown in the following example:</p>
<prE>
import copy
b = [1, 2, [3, 4] ]
a = copy.deepcopy(b) </pre>
</FONT>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, © 2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="37.html" title="Reference Counting and Garbage Collection"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=38" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="38.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="39.html" title="Built-in Types"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -