Repository navigation
Expand file tree
/
Copy pathFunctions.html
More file actions
282 lines (258 loc) · 18.1 KB
/
Copy pathFunctions.html
File metadata and controls
282 lines (258 loc) · 18.1 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>More on Functions — PythonCert 4.0 documentation</title>
<link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '4.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Documentation" href="Documentation.html" />
<link rel="prev" title="Git Workflow" href="GitWorkflow.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="more-on-functions">
<span id="id1"></span><h1>More on Functions<a class="headerlink" href="#more-on-functions" title="Permalink to this headline">¶</a></h1>
<p>From various tutorials, etc, you should have the basics of writing functions down.</p>
<p>In particular, you know that functions can contain a chunk of code that can be wirtten once, and used multiple times from other parts of the code.</p>
<p>You know that you can pass values into the function, and that it can return values to the “calling” code.</p>
<p>Now we will dig a bit deeper down into the specifics of functions in Python:</p>
<div class="section" id="variable-scope">
<h2>Variable scope<a class="headerlink" href="#variable-scope" title="Permalink to this headline">¶</a></h2>
<p>Defining a function:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="k">return</span> <span class="n">z</span>
</pre></div>
</div>
<p><cite>x</cite>, <cite>y</cite>, <cite>z</cite> are <em>local</em> names.</p>
<p><cite>x</cite> and <cite>y</cite> because there are function <em>parameters</em></p>
<p><cite>z</cite> because it was “bound” inside the function.</p>
</div>
<div class="section" id="local-vs-global">
<h2>Local vs. Global<a class="headerlink" href="#local-vs-global" title="Permalink to this headline">¶</a></h2>
<p>Names bound in Python have a <em>scope</em></p>
<p>That <em>scope</em> determines where a symbol is visible, or what value it has in a
given block.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </span><span class="n">x</span> <span class="o">=</span> <span class="mi">32</span>
<span class="gp">In [15]: </span><span class="n">y</span> <span class="o">=</span> <span class="mi">33</span>
<span class="gp">In [16]: </span><span class="n">z</span> <span class="o">=</span> <span class="mi">34</span>
<span class="gp">In [17]: </span><span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">):</span>
<span class="gp"> ....: </span> <span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span>
<span class="gp"> ....:</span>
<span class="gp">In [18]: </span><span class="n">fun</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="go">32 3 4</span>
</pre></div>
</div>
<p>x is global, y and z local to the function</p>
<p>But, did the value of y and z change in the <em>global</em> scope?</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [19]: </span><span class="n">y</span>
<span class="gh">Out[19]: </span><span class="go">33</span>
<span class="gp">In [20]: </span><span class="n">z</span>
<span class="gh">Out[20]: </span><span class="go">34</span>
</pre></div>
</div>
<p>No – they did not. The “y” and “z” names <em>inside</em> the function are completely separate from the “y” and “z” outside the function.</p>
<p>The ones outside the function are “global” names.</p>
<p><strong>NOTE:</strong> “global” in python means global to the module (generally a single file), not global to an entire program. Which is really good, as you have little way of knowing what names are being used in packages you are using, but are not writing yourself!</p>
<p>In general, you should use global bindings mostly for constants.</p>
<p>The python convention is to designate global constants by typing the
names we bind to them in ALL_CAPS:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">INSTALLED_APPS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'baz'</span><span class="p">]</span>
<span class="n">CONFIGURATION_KEY</span> <span class="o">=</span> <span class="s1">'some secret value'</span>
<span class="o">...</span>
</pre></div>
</div>
<p>This is just a convention, but it’s a good one to follow.</p>
</div>
<div class="section" id="global-gotcha">
<h2>Global Gotcha<a class="headerlink" href="#global-gotcha" title="Permalink to this headline">¶</a></h2>
<p>Take a look at this function definition:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [21]: </span><span class="n">x</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">In [22]: </span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
<span class="gp"> ....: </span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span>
<span class="gp"> ....: </span> <span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp"> ....: </span> <span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp"> ....: </span> <span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="gp"> ....:</span>
</pre></div>
</div>
<p>What is going to happen when we call <code class="docutils literal"><span class="pre">f</span></code>?</p>
<p>Try it and see:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [34]: </span><span class="n">f</span><span class="p">()</span>
<span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">UnboundLocalError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="nn"><ipython-input-34-0ec059b9bfe1></span> in <span class="ni"><module></span><span class="nt">()</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="n">f</span><span class="p">()</span>
<span class="nn"><ipython-input-33-4363b2b69f73></span> in <span class="ni">f</span><span class="nt">()</span>
<span class="g g-Whitespace"> </span><span class="mi">1</span> <span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
<span class="ne">----> </span><span class="mi">2</span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span>
<span class="g g-Whitespace"> </span><span class="mi">3</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
<span class="g g-Whitespace"> </span><span class="mi">4</span> <span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="g g-Whitespace"> </span><span class="mi">5</span> <span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="ne">UnboundLocalError</span>: local variable 'x' referenced before assignment
</pre></div>
</div>
<p>Because you are binding the symbol <code class="docutils literal"><span class="pre">x</span></code> locally, it becomes a local and masks
the global value already bound.</p>
</div>
<div class="section" id="parameters">
<h2>Parameters<a class="headerlink" href="#parameters" title="Permalink to this headline">¶</a></h2>
<p>So far we’ve seen simple parameter lists:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span>
</pre></div>
</div>
<p>These types of parameters are called <em>positional</em></p>
<p>When you call a function, you <strong>must</strong> provide arguments for all <em>positional</em>
parameters <em>in the order they are listed</em></p>
<p>You can provide <em>default values</em> for parameters in a function definition:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [24]: </span><span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
<span class="gp"> ....: </span> <span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span>
<span class="gp"> ....:</span>
</pre></div>
</div>
<p>When parameters are given with default values, they become <em>optional</em></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="n">fun</span><span class="p">()</span>
<span class="go">1 2 3</span>
</pre></div>
</div>
<p>You can provide arguments to a function call for <em>optional</em> parameters
positionally:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [26]: </span><span class="n">fun</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="go">6 2 3</span>
<span class="gp">In [27]: </span><span class="n">fun</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
<span class="go">6 7 3</span>
<span class="gp">In [28]: </span><span class="n">fun</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
<span class="go">6 7 8</span>
</pre></div>
</div>
<p>Or, you can use the parameter name as a <em>keyword</em> to indicate which you mean:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [29]: </span><span class="n">fun</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">1 4 3</span>
</pre></div>
</div>
<p>Once you’ve provided a <em>keyword</em> argument in this way, you can no longer
provide any <em>positional</em> arguments:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [30]: </span><span class="n">fun</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="gt"> File</span><span class="nn"> "<ipython-input-30-4529e5befb95>"</span><span class="gt">, line </span><span class="mi">1</span>
<span class="n">fun</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="ne">SyntaxError</span>: non-keyword arg after keyword arg
</pre></div>
</div>
</div>
<div class="section" id="recursion">
<h2>Recursion<a class="headerlink" href="#recursion" title="Permalink to this headline">¶</a></h2>
<p>You’ve seen functions that call other functions.</p>
<p>If a function calls <em>itself</em>, we call that <strong>recursion</strong></p>
<p>Like with other functions, a call within a call establishes a <em>call stack</em></p>
<p>With recursion, if you are not careful, this stack can get <em>very</em> deep.</p>
<p>Python has a maximum limit to how much it can recurse. This is intended to
save your machine from running out of RAM.</p>
</div>
<div class="section" id="recursion-can-be-useful">
<h2>Recursion can be Useful<a class="headerlink" href="#recursion-can-be-useful" title="Permalink to this headline">¶</a></h2>
<p>Recursion is especially useful for a particular set of problems.</p>
<p>For example, take the case of the <em>factorial</em> function.</p>
<p>In mathematics, the <em>factorial</em> of an integer is the result of multiplying that integer by every integer smaller than itself down to 1.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>5! == 5 * 4 * 3 * 2 * 1
</pre></div>
</div>
<p>We can use a recursive function nicely to model this mathematical function</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fact</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="sd">"""compute the factorial of the input value, n"""</span>
<span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="mi">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fact</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>This is a typical structure for a recursive function:</p>
<ol class="upperalpha simple">
<li>It starts with a check to see if there if the recursive process is “done” – can it simply return a simple value.</li>
<li>If not, then it does a computation using the same function of another value.</li>
</ol>
<p>It is critical at that that first step be there, or the function will never terminate.</p>
<blockquote>
<div>[demo]</div></blockquote>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">More on Functions</a><ul>
<li><a class="reference internal" href="#variable-scope">Variable scope</a></li>
<li><a class="reference internal" href="#local-vs-global">Local vs. Global</a></li>
<li><a class="reference internal" href="#global-gotcha">Global Gotcha</a></li>
<li><a class="reference internal" href="#parameters">Parameters</a></li>
<li><a class="reference internal" href="#recursion">Recursion</a></li>
<li><a class="reference internal" href="#recursion-can-be-useful">Recursion can be Useful</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li>Previous: <a href="GitWorkflow.html" title="previous chapter">Git Workflow</a></li>
<li>Next: <a href="Documentation.html" title="next chapter">Documentation</a></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/modules/Functions.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2017, Christopher Barker, Cris Ewing, Christy Heaton, Maria McKinley, Rick Riehle, Joseph Schilz.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.2</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
|
<a href="../_sources/modules/Functions.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>