📄 32.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 Developer's Handbook -> Functions and Procedures</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.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="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="22.html" class="navtitle">2. Language Review</a> > <span class="nonavtitle">Functions and Procedures</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="31.html" title="Data Structures"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=32" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="32.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="33.html" title="Modules and Packages"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A19%3A53+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162145080038234097053096052</font><a href="read3.asp?bookname=0672319942&snode=32&now=5%2F31%2F2002+4%3A19%3A53+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Functions and Procedures</h3>
<p>Functions and procedures are blocks of code that you can access from several different parts of your code. As you already know, Python gives you some <tT CLAss="monofont">built-in</tt> functions, but you can also create your own functions. Yours are called <A NAMe="idx1073742460"></a>
<a nAME="idx1073742461"></A>
<tt class="monofont">user-defined functions.</tt> Functions and procedures provide better modularity for your application and a high degree of code reusing.</p>
<p>Procedures are functions that don't return a value. The only difference between a function and a procedure is that a procedure has either a <a name="idx1073742462"></a>
<a naMe="idx1073742463"></a>
<Tt clAss="monofont">return</tt> command without arguments (that returns <Tt clASS="monofont">None</Tt>), or it doesn't have any return statement. From now on, I will use only the word <tt cLASS="monofont">function.</tt>
</p>
<p>While functions are being executed, they create their own <A NAMe="idx1073742464"></a>
<a nAME="idx1073742465"></A>
<tt class="monofont">namespace.</tt>
</p>
<p>Every time you invoke a function, such as <i>
<tt class="monofont">function</tT>
</i>
<tT claSs="monofont">(a,b,c)</tt>
</p>
<Ul>
<li>
<P>Python does a search within its <TT Class="monofont">namespaces</TT> looking for <I>
<Tt claSS="monofont">function</TT>
</i> to identify whether this is a python object.</p>
</li>
<LI>
<P>Python creates a tuple of the <I>arguments</i> that were passed. Following our example, we have <i>arguments</i>=(a,b,c).</p>
</li>
<li>
<p>Python invokes the function internally like this: <tt class="monofont">apply(</tt>
<i>
<tT clAss="monofont">function,arguments</tT>
</i>
<tt cLass="monofont">).</TT>
</P>
</Li>
</ul>
<p>As you can see, tuples are an unavoidable concept inside the language.</P>
<P>Python, by nature, allows introspection to an unprecedented degree. You can separate a function name from its parameters, store them in some place, play around with them, and later use the <TT clasS="monofont">apply built-in</TT> function to execute the function.</P>
<h4>Functions</h4>
<p>Functions always start with the abbreviation <a NAME="idx1073742466"></a>
<a name="idx1073742467"></a>
<tt class="monofont">def.</tt> Their end is defined by the last line of the indented block of code that goes underneath.</p>
<p>The general <a nAme="idx1073742468"></A>
<a naMe="idx1073742469"></a>format of a function is as follows:</p>
<pRe>
def functionname(<i>arg1, arg2,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -