Skip to content

Commit 6c0a137

Browse files
author
Mark Pilgrim
committed
moved pattern_file back to __init__ in plural6.py
1 parent 62aa447 commit 6c0a137

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

diveintopython3.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* DONE 2nd draft Case Study: Porting chardet to Python 3
4444
* Where to go from here
4545
* DONE 2nd draft Porting Code to Python 3 with 2to3
46-
* TODO 2nd draft Special Method Names
46+
* DONE 2nd draft Special Method Names
4747
* Bits to add somewhere
4848
** DONE section on tuples
4949
** TODO section on dictionary views

examples/plural6.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ def apply_rule(word):
1717
class LazyRules:
1818
rules_filename = 'plural6-rules.txt'
1919

20-
def __iter__(self):
20+
def __init__(self):
2121
self.pattern_file = open(self.rules_filename, encoding='utf-8')
22+
23+
def __iter__(self):
2224
self.cache = []
2325
self.cache_index = 0
2426
return self

installing-python.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ <h2 id=idle>Using The Python Shell</h2>
346346
<li>The prompt changes back to <samp class=p>>>></samp> to signal that you&#8217;ve left the interactive help mode and returned to the Python Shell.
347347
</ol>
348348

349-
<p><abbr>IDLE</abbr>, the graphical Python Shell, also includes a Python-aware text editor. You&#8217;ll see how to use it in the next chapter.
349+
<p><abbr>IDLE</abbr>, the graphical Python Shell, also includes a Python-aware text editor. You&#8217;ll see how to use it in the next section.
350350

351351
<p class=a>&#x2042;
352352

iterators.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ <h2 id=a-plural-rule-iterator>A Plural Rule Iterator</h2>
217217
<pre><code class=pp>class LazyRules:
218218
rules_filename = 'plural6-rules.txt'
219219

220-
def __iter__(self):
220+
def __init__(self):
221221
self.pattern_file = open(self.rules_filename, encoding='utf-8')
222+
223+
def __iter__(self):
222224
self.cache = []
223225
self.cache_index = 0
224226
return self
@@ -251,14 +253,16 @@ <h2 id=a-plural-rule-iterator>A Plural Rule Iterator</h2>
251253
<pre><code class=pp>class LazyRules:
252254
rules_filename = 'plural6-rules.txt'
253255

254-
<a> def __iter__(self): <span class=u>&#x2460;</span></a>
255-
<a> self.pattern_file = open(self.rules_filename, encoding='utf-8') <span class=u>&#x2461;</span></a>
256+
def __init__(self):
257+
<a> self.pattern_file = open(self.rules_filename, encoding='utf-8') <span class=u>&#x2460;</span></a>
258+
259+
<a> def __iter__(self): <span class=u>&#x2461;</span></a>
256260
<a> self.cache = [] <span class=u>&#x2462;</span></a>
257261
self.cache_index = 0</code></pre>
258262
<ol>
259-
<li>The <code>__iter__()</code> method is only going to be called once, after you instantiate the class, assign it to <var>rules</var>, and call <code>iter(rules)</code> to create the iterator.
260-
<li>Since this is only going to get called once, it&#8217;s the perfect place to open the pattern file. No point doing more than you absolutely have to until absolutely necessary!
261-
<li>Also, this is a good place to initialize the cache, which you&#8217;ll use later as you read the patterns from the pattern file.
263+
<li>When we instantiate the <code>LazyRules</code> class, open the pattern file but don&#8217;t read anything from it. (That comes later.)
264+
<li>The <code>__iter__()</code> method is going to be called after you instantiate the class, assign it to <var>rules</var>, and call <code>iter(rules)</code> to create the iterator. It would also get called again if you created a new iterator from the same <var>rules</var> object.
265+
<li>That means that this is a right place to initialize the cache and the cache index position. You&#8217;ll use these later (in the <code>__next__()</code> method) as you read the patterns from the pattern file.
262266
</ol>
263267

264268
<p>Before we continue, let&#8217;s take a closer look at <var>rules_filename</var>. It&#8217;s not defined within the <code>__iter__()</code> method. In fact, it&#8217;s not defined within <em>any</em> method. It&#8217;s defined at the class level. It&#8217;s a <i>class variable</i>, and although you can access it just like an instance variable (<var>self.rules_filename</var>), it is shared across all instances of the <code>LazyRules</code> class.

table-of-contents.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ <h1>Table of Contents</h1>
3838
<ol>
3939
<li><a href=your-first-python-program.html#docstrings>Docstrings</a>
4040
</ol>
41+
<li><a href=your-first-python-program.html#importsearchpath>The <code>import</code> search path</a>
4142
<li><a href=your-first-python-program.html#everythingisanobject>Everything is an object</a>
4243
<ol>
43-
<li><a href=your-first-python-program.html#importsearchpath>The <code>import</code> search path</a>
4444
<li><a href=your-first-python-program.html#whatsanobject>What&#8217;s an object?</a>
4545
</ol>
4646
<li><a href=your-first-python-program.html#indentingcode>Indenting code</a>

0 commit comments

Comments
 (0)