File: optional_arguments.html

package info (click to toggle)
diveintopython 5.4-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 4,116 kB
  • ctags: 2,838
  • sloc: python: 4,417; xml: 894; makefile: 29
file content (130 lines) | stat: -rw-r--r-- 11,483 bytes parent folder | download | duplicates (2)
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
127
128
129
130

<!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=iso-8859-1">
   
      <title>4.2.&nbsp;Using Optional and Named Arguments</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="Chapter&nbsp;4.&nbsp;The Power Of Introspection">
      <link rel="previous" href="index.html" title="Chapter&nbsp;4.&nbsp;The Power Of Introspection">
      <link rel="next" href="built_in_functions.html" title="4.3.&nbsp;Using type, str, dir, and Other Built-In Functions">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">You are here: <a href="../index.html">Home</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">The Power Of Introspection</a>&nbsp;&gt;&nbsp;<span class="thispage">Using Optional and Named Arguments</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="index.html" title="Prev: &#8220;The Power Of Introspection&#8221;">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="built_in_functions.html" title="Next: &#8220;Using type, str, dir, and Other Built-In Functions&#8221;">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">Dive Into Python</a></h1>
               <p id="tagline">Python from novice to pro</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:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=" "> <input type="submit" value="Search"><input type="hidden" name="cof" value="LW:752;L:http://diveintopython.org/images/diveintopython.png;LH:42;AH:left;GL:0;AWFID:3ced2bb1f7f1b212;"><input type="hidden" name="domains" value="diveintopython.org"><input type="hidden" name="sitesearch" value="diveintopython.org"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="en">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="apihelper.optional"></a>4.2.&nbsp;Using Optional and Named Arguments
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p><span class="application">Python</span> allows function arguments to have default values; if the function is called without the argument, the argument gets its default
               value.  Futhermore, arguments can be specified in any order by using named arguments.  Stored procedures in <span class="application">SQL Server</span> Transact/<span class="acronym">SQL</span> can do this, so if you're a <span class="application">SQL Server</span> scripting guru, you can skim this part.
            </p>
         </div>
         <div class="informalexample">
            <p>Here is an example of <tt class="function">info</tt>, a function with two optional arguments:
            </p><pre class="programlisting"><span class='pykeyword'>
def</span> info(object, spacing=10, collapse=1):</pre></div>
         <p><tt class="varname">spacing</tt> and <tt class="varname">collapse</tt> are optional, because they have default values defined.  <tt class="varname">object</tt> is required, because it has no default value.  If <tt class="function">info</tt> is called with only one argument, <tt class="varname">spacing</tt> defaults to <tt class="constant">10</tt> and <tt class="varname">collapse</tt> defaults to <tt class="constant">1</tt>.  If <tt class="function">info</tt> is called with two arguments, <tt class="varname">collapse</tt> still defaults to <tt class="constant">1</tt>.
         </p>
         <p>Say you want to specify a value for <tt class="varname">collapse</tt> but want to accept the default value for <tt class="varname">spacing</tt>.  In most languages, you would be out of luck, because you would need to call the function with three arguments.  But in
            <span class="application">Python</span>, arguments can be specified by name, in any order.
         </p>
         <div class="example"><a name="d0e8401"></a><h3 class="title">Example&nbsp;4.4.&nbsp;Valid Calls of <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">With only one argument, <tt class="varname">spacing</tt> gets its default value of <tt class="literal">10</tt> and <tt class="varname">collapse</tt> gets its default value of <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">With two arguments, <tt class="varname">collapse</tt> gets its default value of <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">Here you are naming the <tt class="varname">collapse</tt> argument explicitly and specifying its value.  <tt class="varname">spacing</tt> still gets its default value of <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">Even required arguments (like <tt class="varname">object</tt>, which has no default value) can be named, and named arguments can appear in any order.
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>This looks totally whacked until you realize that arguments are simply a dictionary.  The &#8220;<span class="quote">normal</span>&#8221; method of calling functions without argument names is actually just a shorthand where <span class="application">Python</span> matches up the values with the argument names in the order they're specified in the function declaration.  And most of the
            time, you'll call functions the &#8220;<span class="quote">normal</span>&#8221; way, but you always have the additional flexibility if you need it.
         </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="Note" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%">The only thing you need to do to call a function is specify a value (somehow) for each required argument; the manner and order
                  in which you do that is up to you.
               </td>
            </tr>
         </table>
         <div class="furtherreading">
            <h3>Further Reading on Optional Arguments</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> discusses exactly <a href="http://www.python.org/doc/current/tut/node6.html#SECTION006710000000000000000">when and how default arguments are evaluated</a>, which matters when the default value is a list or an expression with side effects.
               </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">&lt;&lt;&nbsp;The Power Of Introspection</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#apihelper.divein" title="4.1.&nbsp;Diving In">1</a> <span class="divider">|</span> <span class="thispage">2</span> <span class="divider">|</span> <a href="built_in_functions.html" title="4.3.&nbsp;Using type, str, dir, and Other Built-In Functions">3</a> <span class="divider">|</span> <a href="getattr.html" title="4.4.&nbsp;Getting Object References With getattr">4</a> <span class="divider">|</span> <a href="filtering_lists.html" title="4.5.&nbsp;Filtering Lists">5</a> <span class="divider">|</span> <a href="and_or.html" title="4.6.&nbsp;The Peculiar Nature of and and or">6</a> <span class="divider">|</span> <a href="lambda_functions.html" title="4.7.&nbsp;Using lambda Functions">7</a> <span class="divider">|</span> <a href="all_together.html" title="4.8.&nbsp;Putting It All Together">8</a> <span class="divider">|</span> <a href="summary.html" title="4.9.&nbsp;Summary">9</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="built_in_functions.html">Using type, str, dir, and Other Built-In Functions&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright &copy; 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
      </div>
   </body>
</html>