⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 22.html

📁 国外python经典教材,python爱好者的首选
💻 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 -&gt; Functions</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> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="12.html" class="navtitle">1. A Tutorial Introduction</a> &gt; <span class="nonavtitle">Functions</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="21.html" title="Dictionaries"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=22" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="22.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="23.html" title="Classes"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A33%3A59+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137110162165015143224220233</font><a href="read0.asp?bookname=0735710910&snode=22&now=5%2F28%2F2002+8%3A33%3A59+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Functions</h3>
<p>You use the <tT CLAss="monofont">def</tt>  statement to create a function, as shown in the following example:</P>

<PRE>

def remainder(a,b): 
        q = a/b 
        r = a - q*b 
        return r </pre>

<p>To invoke a function, simply use the name of the function followed by its arguments enclosed in parentheses, such as <TT CLass="monofont">result = remainder(37,15)</tt>. You can use a tuple to return multiple values from a function, as in the following example:</p>

<pre>

def divide(a,b): 
        q = a/b        # If a and b are integers, q is an integer 
        r = a - q*b 
        return (q,r) </pre>

<p>When returning multiple values in a tuple, it抯 often useful to invoke the function as follows:</p>

<pre>

quotient, remainder = divide(1456,33) </pre>

<P>To assign a default value to a parameter, use assignment:</p>

<pRe>

def connect(hostname,port,timeout=300): 
      # Function body </prE>

<p>When default values are given in a function definition, they can be omitted from subsequent function calls. For example:</p>

<prE>

connect('www.python.org', 80) </pre>

<P>You also can invoke functions by using keyword arguments and supplying the arguments in arbitrary order. For example:</P>

<PRe>

connect(port=80,hostname="www.python.org") </pre>

<P>When variables are created or assigned inside a function, their scope is local. To modify the value of a global variable from inside a function, use the <TT Class="monofont">global</TT> statement as follows:</P>

<Pre>

a = 4.5 
... 
def foo(): 
        global a 
        a = 8.8             # Changes the global variable a </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, &copy;&nbsp;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="21.html" title="Dictionaries"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=22" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="22.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="23.html" title="Classes"><font size="1">CONTINUE&nbsp;&gt;</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 + -