📄 64.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 -> Parameter Passing and Return Values</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="61.html" class="navtitle">6. Functions and Functional Programming</a> > <span class="nonavtitle">Parameter Passing and Return Values</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="63.html" title="Functions"><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=64" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="64.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="65.html" title="Scoping Rules"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A58%3A43+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104016132047023109078082</font><a href="read6.asp?bookname=0735710910&snode=64&now=5%2F28%2F2002+8%3A58%3A43+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Parameter Passing and Return Values</h3>
<p>When a function is invoked, its parameters are passed by reference. If a mutable object (such as a list or dictionary) is passed to a function where it抯 then modified, those changes will be reflected in the caller. For example:</p>
<PRE>
a = [1,2,3,4,5]
def foo(x):
x[3] = -55 # Modify an element of x
foo(a) # Pass a
print a # Produces [1,2,3,-55,5] </Pre>
<p>The <tT CLAss="monofont">return</tt> statement returns a value from a function. If no value is specified or you omit the <TT CLass="monofont">return</tt> statement, the <tt class="monofont">None</tt> object is returned. To return multiple values, place them in a tuple:</p>
<pre>
def factor(a):
d = 2
while (d <= (a/2)):
if ((a/d)*d == a):
return ((a/d),d)
d = d + 1
return (a,1) </prE>
<p>Multiple return values returned in a tuple can be assigned to individual variables:</p>
<Pre>
x,y = factor(1243) # Return values placed in x and y.
(x,y) = factor(1243) # Alternate version. Same behavior. </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="63.html" title="Functions"><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=64" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="64.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="65.html" title="Scoping Rules"><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 + -