-
Notifications
You must be signed in to change notification settings - Fork 0
/
little-lessons-episode-2.html
240 lines (208 loc) · 12.5 KB
/
little-lessons-episode-2.html
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
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<!-- Set the viewport width to device width for mobile -->
<meta name="viewport" content="width=device-width" />
<title>Little Lessons: Episode 2</title>
<link rel="stylesheet" href="http://lmontopo.github.io/theme/css/normalize.css" />
<link rel="stylesheet" href="http://lmontopo.github.io/theme/css/foundation.min.css" />
<link rel="stylesheet" href="http://lmontopo.github.io/theme/css/style.css" />
<link rel="stylesheet" href="http://lmontopo.github.io/theme/css/pygments.css" />
<script src="http://lmontopo.github.io/theme/js/modernizr.js"></script>
</head>
<body>
<!-- Nav Bar -->
<nav>
<div class="top-bar">
<div class="row">
<div class="large-9 large-centered columns">
<h1><a href="http://lmontopo.github.io">The L Blog</a></h1>
</div>
</div>
</div>
<!-- Show menu items and pages -->
<div class="row">
<div class="large-9 columns">
<ul class="button-group navigation">
</ul>
</div>
</div>
</nav>
<!-- End Nav -->
<!-- Main Page Content and Sidebar -->
<div class="row">
<!-- Main Blog Content -->
<div class="large-9 columns">
<article>
<header>
<h3 class="article-title"><a href="http://lmontopo.github.io/little-lessons-episode-2.html" rel="bookmark"
title="Permalink to Little Lessons: Episode 2">Little Lessons: Episode 2</a></h3>
</header>
<h6 class="subheader" title="2014-11-06T05:00:00-05:00">Thu 06 November 2014
</h6>
<p>Seeing as there are a whole <strong>bunch</strong> of "little lessons" that I'd like
to write down and keep track of, I've decided to extend my previous blog
post "Little Lessons" into a whole series! Get ready!</p>
<h4>Mutating Iterables:</h4>
<p>Suppose we have a list and we'd like to mutate some (or all) of its
elements as we iterate through them. Supposing that the way in which we
want to mutate each element isn't too complicated, this seems like a
straight forward task.</p>
<p>The case I was initially looking at consisted of a list where each
element of my list was either True or False. I wanted to go through the
list and change some of the False entries to True. As is the case with
many tasks in computing, this task was a little trickier than I
initially expected. For the purpose of this lesson, it will suffice to
consider a simple list of integers. Here we go!</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">>>></span> <span class="nb">list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</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="o">>>></span> <span class="k">print</span> <span class="nb">list</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</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="o">>>></span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">:</span>
<span class="o">...</span> <span class="n">item</span> <span class="o">+=</span><span class="mi">2</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="k">print</span> <span class="nb">list</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>I'm not sure about you, but this was not exactly what I was expecting
the first time I tried it. Lets add an extra print statement to better
see whats going on.</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">>>></span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">:</span>
<span class="o">...</span> <span class="n">item</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="o">...</span> <span class="k">print</span> <span class="n">item</span> <span class="mi">2345</span>
</pre></div>
</td></tr></table>
<p>It seems that our code is understanding, and executing, "item += 1",
yet, the list itself is not being mutated! HOW CAN THIS BE SO?! After a
bit of research Julia and I found our answer: when items of a list are
iterated with this 'for item in list' statement, Python creates an
element 'item' which is a copy of the value of that list element. This
item is an entirely separate object than the elements of the list, and
so changing 'item' has no effect on our list. Here's how you <em>can</em>
change the elements in the list, as intended:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre><span class="o">>>></span> <span class="nb">list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</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="o">>>></span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">)):</span>
<span class="o">...</span> <span class="nb">list</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="nb">list</span>
<span class="p">[</span><span class="mi">2</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="mi">5</span><span class="p">]</span>
</pre></div>
</td></tr></table>
<p>Yay! Notice how we access the elements of the list DIRECTLY.</p>
<h4>User-defined Exceptions/REPL/Errors vs Exceptions</h4>
<p>Yesterday I added the finishing touches to my Lisp Interpreter. (Expect
a long and detailed post about that very soon!) My program was
interpreting Scheme as I intended it to, but it wasn't very user
friendly. I decided to turn it into a REPL. The acccronym REPL stands
for "Read, Evaluate, Print, Loop". What a REPL does is exactly what you
might think: it will read user input, evaluate it, print the result back
to the user, and then wait for further input. Using a while loop and
using Python's raw_input() command, it was pretty straightforward to
get my interpreter to work like a REPL for non-erroneous user input.
However, if the input was erroneous then the entire program crashed.
Somehow, I needed my program to ACCEPT erroneous input and respond to it
with an appropriate error message. The way I went about doing this was
by creating my own class of Exceptions! That's right: since everything
in Python is an object, Errors and Exceptions are themselves objects,
and we can create our very own classes of them!</p>
<p>Lets step back for a minute and quickly discuss what is meant by the
terms "Error" and "Exception" in Python. I found the distinction to be a
little confusing at first. It turns out that the term "Error" is a broad
category which can be divided into two different types: Syntax Errors
and Exceptions. Syntax errors are errors which are produced because
characters or strings are misplaced, missing, or added when they
shouldn't be. In comparison, the errors which occur during execution are
refered to as "exceptions". These errors indicate that the <em>meaning</em> (as
opposed to the syntax) of the code is erroneous.</p>
<p>Great, now lets talk about how I implemented my own class of exceptions!
The implimentation of this class required very little code:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">msg</span> <span class="o">=</span> <span class="n">msg</span>
</pre></div>
</td></tr></table>
<p>Then I created several different instances of this class. Each instance
refers to a different type of error that could occur, and each had a
message associated with it. Here is the function I implement in my
program which takes care of all things to do with being a REPL:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">def</span> <span class="nf">repl</span><span class="p">(</span><span class="n">env</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">x</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s">'> '</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">print</span> <span class="n">interpret</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
<span class="k">except</span> <span class="n">MyError</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">colored</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">msg</span><span class="p">,</span> <span class="s">'red'</span><span class="p">))</span>
<span class="k">return</span> <span class="n">repl</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
<span class="k">except</span> <span class="n">KeyboardIterrupt</span><span class="p">:</span>
<span class="nb">exit</span><span class="p">()</span>
</pre></div>
</td></tr></table>
<p>Here my code "tries" to print out the result of calling the interpret
function on the user input. If any isntance of MyError occurs, then it
will instead return the message associated to that instance of MyError.
Notice the second parameter, 'e'. It makes reference to the particular
instance of MyError which was raised and saves me from having to specify
case by case all of the instances of MyError which could be encountered.
For the record, 'e' is not some MAGIC variable that has a special
meaning in Python. It is simply what I chose to name the second
parameter. I could easily change 'e' to 'blah' and the code will work
the same. (I actually tested it out to check!)</p>
<p class="subheader">Category: <a href="http://lmontopo.github.io/category/blog.html">Blog</a>
Tagged:
<a href="http://lmontopo.github.io/tag/python.html">Python </a>
</p>
</article>
</div>
<!-- End Main Content -->
<!-- Sidebar -->
<aside class="large-3 columns">
<h5 class="sidebar-title">Site</h5>
<ul class="side-nav">
<li><a href="http://lmontopo.github.io/archives.html">Archives</a>
<li><a href="http://lmontopo.github.io/tags.html">Tags</a>
</ul>
<h5 class="sidebar-title">Categories</h5>
<ul class="side-nav">
<li><a href="http://lmontopo.github.io/category/about.html">About</a></li>
<li><a href="http://lmontopo.github.io/category/blog.html">Blog</a></li>
</ul>
</aside> <!-- End Sidebar -->
</div> <!-- End Main Content and Sidebar -->
<!-- Footer -->
<footer class="row">
<div class="large-12 columns">
<hr />
<div class="row">
<div class="large-6 columns">
<!-- <p>The L Blog by Leta Montopoli</p> -->
</div>
</div>
</div>
</footer>
</body>
</html>