/usr/share/doc/diveintopython-zh/html/power_of_introspection/optional_arguments.html is in diveintopython-zh 5.4b-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | <!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>4.2. 使用可选参数和命名参数</title>
<link rel="stylesheet" href="../diveintopython.css" type="text/css">
<link rev="made" href="mailto:f8dy@diveintopython.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
<meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
<meta name="description" content="Python from novice to pro">
<link rel="home" href="../toc/index.html" title="Dive Into Python">
<link rel="up" href="index.html" title="第 4 章 自省的威力">
<link rel="previous" href="index.html" title="第 4 章 自省的威力">
<link rel="next" href="built_in_functions.html" title="4.3. 使用 type、str、dir 和其它内置函数">
</head>
<body>
<table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td id="breadcrumb" colspan="5" align="left" valign="top">导航:<a href="../index.html">起始页</a> > <a href="../toc/index.html">Dive Into Python</a> > <a href="index.html">自省的威力</a> > <span class="thispage">使用可选参数和命名参数</span></td>
<td id="navigation" align="right" valign="top"> <a href="index.html" title="上一页: “自省的威力”"><<</a> <a href="built_in_functions.html" title="下一页: “使用 type、str、dir 和其它内置函数”">>></a></td>
</tr>
<tr>
<td colspan="3" id="logocontainer">
<h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
<p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
</td>
<td colspan="3" align="right">
<form id="search" method="GET" action="http://www.google.com/custom">
<p><label for="q" accesskey="4">Find: </label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
</form>
</td>
</tr>
</table>
<!--#include virtual="/inc/ads" -->
<div class="section" lang="zh_cn">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="apihelper.optional"></a>4.2. 使用可选参数和命名参数
</h2>
</div>
</div>
<div></div>
</div>
<div class="abstract">
<p><span class="application">Python</span> 允许函数参数有缺省值;如果调用函数时不使用参数,参数将获得它的缺省值。此外,通过使用命名参数还可以以任意顺序指定参数。<span class="application">SQL Server</span> Transact/<span class="acronym">SQL</span> 中的存储过程也可以做到这些;如果你是脚本高手,你可以略过这部分。
</p>
</div>
<div class="informalexample">
<p><tt class="function">info</tt> 函数就是这样一个例子,它有两个可选参数。
</p><pre class="programlisting"><span class='pykeyword'>
def</span> info(object, spacing=10, collapse=1):</pre></div>
<p><tt class="varname">spacing</tt> 和 <tt class="varname">collapse</tt> 是可选参数,因为它们已经定义了缺省值。<tt class="varname">object</tt> 是必备参数,因为它没有指定缺省值。如果调用 <tt class="function">info</tt> 时只指定一个参数,那么 <tt class="varname">spacing</tt> 缺省为 <tt class="constant">10</tt> ,<tt class="varname">collapse</tt> 缺省为 <tt class="constant">1</tt>。如果调用 <tt class="function">info</tt> 时指定两个参数,<tt class="varname">collapse</tt> 依然默认为 <tt class="constant">1</tt>。
</p>
<p>假如你要指定 <tt class="varname">collapse</tt> 的值,但是又想要接受 <tt class="varname">spacing</tt> 的缺省值。在绝大部分语言中,你可能运气就不太好了,因为你需要使用三个参数来调用函数,这势必要重新指定 <tt class="varname">spacing</tt> 的值。但是在 <span class="application">Python</span> 中,参数可以通过名称以任意顺序指定。
</p>
<div class="example"><a name="d0e8524"></a><h3 class="title">例 4.4. <tt class="function">info</tt> 的有效调用
</h3><pre class="programlisting">
info(odbchelper) <a name="apihelper_args.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
info(odbchelper, 12) <a name="apihelper_args.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
info(odbchelper, collapse=0) <a name="apihelper_args.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
info(spacing=15, object=odbchelper) <a name="apihelper_args.1.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#apihelper_args.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">只使用一个参数,<tt class="varname">spacing</tt> 使用缺省值 <tt class="literal">10</tt> ,<tt class="varname">collapse</tt> 使用缺省值 <tt class="constant">1</tt>。
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#apihelper_args.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">使用两个参数,<tt class="varname">collapse</tt> 使用缺省值 <tt class="constant">1</tt>。
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#apihelper_args.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">这里你显式命名了 <tt class="varname">collapse</tt> 并指定了它的值。<tt class="varname">spacing</tt> 将依然使用它的缺省值 <tt class="literal">10</tt>。
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#apihelper_args.1.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">甚至必备参数 (例如 <tt class="varname">object</tt>,没有指定缺省值) 也可以采用命名参数的方式,而且命名参数可以以任意顺序出现。
</td>
</tr>
</table>
</div>
</div>
<p>这些看上去非常累,除非你意识到参数不过是一个字典。“<span class="quote">通常</span>” 不使用参数名称的函数调用只是一个简写的形式,<span class="application">Python</span> 按照函数声明中定义的的参数顺序将参数值和参数名称匹配起来。大部分时间,你会使用“<span class="quote">通常</span>”方式调用函数,但是如果你需要,总是可以提供附加的灵活性。
</p><a name="tip.arguments"></a><table class="note" border="0" summary="">
<tr>
<td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="注意" title="" width="24" height="24"></td>
</tr>
<tr>
<td colspan="2" align="left" valign="top" width="99%">调用函数时唯一必须做的事情就是为每一个必备参数指定值 (以某种方式);以何种具体的方式和顺序都取决于你。</td>
</tr>
</table>
<div class="furtherreading">
<h3>进一步阅读</h3>
<ul>
<li><a href="http://www.python.org/doc/current/tut/tut.html"><i class="citetitle"><span class="application">Python</span> Tutorial</i></a> 确切地讨论了<a href="http://www.python.org/doc/current/tut/node6.html#SECTION006710000000000000000">何时、如何进行缺省参数赋值</a>,这都和缺省值是一个 list 还是一个具有副作用的表达式有关。
</li>
</ul>
</div>
</div>
<table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td width="35%" align="left"><br><a class="NavigationArrow" href="index.html"><< 自省的威力</a></td>
<td width="30%" align="center"><br> <span class="divider">|</span> <a href="index.html#apihelper.divein" title="4.1. 概览">1</a> <span class="divider">|</span> <span class="thispage">2</span> <span class="divider">|</span> <a href="built_in_functions.html" title="4.3. 使用 type、str、dir 和其它内置函数">3</a> <span class="divider">|</span> <a href="getattr.html" title="4.4. 通过 getattr 获取对象引用">4</a> <span class="divider">|</span> <a href="filtering_lists.html" title="4.5. 过滤列表">5</a> <span class="divider">|</span> <a href="and_or.html" title="4.6. and 和 or 的特殊性质">6</a> <span class="divider">|</span> <a href="lambda_functions.html" title="4.7. 使用 lambda 函数">7</a> <span class="divider">|</span> <a href="all_together.html" title="4.8. 全部放在一起">8</a> <span class="divider">|</span> <a href="summary.html" title="4.9. 小结">9</a> <span class="divider">|</span>
</td>
<td width="35%" align="right"><br><a class="NavigationArrow" href="built_in_functions.html">使用 type、str、dir 和其它内置函数 >></a></td>
</tr>
<tr>
<td colspan="3"><br></td>
</tr>
</table>
<div class="Footer">
<p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
<p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
</div>
</body>
</html>
|