Skip to content

Commit

Permalink
moved pattern_file back to __init__ in plural6.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Pilgrim committed Jul 27, 2009
1 parent 62aa447 commit 6c0a137
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion diveintopython3.org
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* DONE 2nd draft Case Study: Porting chardet to Python 3
* Where to go from here
* DONE 2nd draft Porting Code to Python 3 with 2to3
* TODO 2nd draft Special Method Names
* DONE 2nd draft Special Method Names
* Bits to add somewhere
** DONE section on tuples
** TODO section on dictionary views
Expand Down
4 changes: 3 additions & 1 deletion examples/plural6.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ def apply_rule(word):
class LazyRules:
rules_filename = 'plural6-rules.txt'

def __iter__(self):
def __init__(self):
self.pattern_file = open(self.rules_filename, encoding='utf-8')

def __iter__(self):
self.cache = []
self.cache_index = 0
return self
Expand Down
2 changes: 1 addition & 1 deletion installing-python.html
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ <h2 id=idle>Using The Python Shell</h2>
<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.
</ol>

<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.
<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.

<p class=a>&#x2042;

Expand Down
16 changes: 10 additions & 6 deletions iterators.html
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ <h2 id=a-plural-rule-iterator>A Plural Rule Iterator</h2>
<pre><code class=pp>class LazyRules:
rules_filename = 'plural6-rules.txt'

def __iter__(self):
def __init__(self):
self.pattern_file = open(self.rules_filename, encoding='utf-8')

def __iter__(self):
self.cache = []
self.cache_index = 0
return self
Expand Down Expand Up @@ -251,14 +253,16 @@ <h2 id=a-plural-rule-iterator>A Plural Rule Iterator</h2>
<pre><code class=pp>class LazyRules:
rules_filename = 'plural6-rules.txt'

<a> def __iter__(self): <span class=u>&#x2460;</span></a>
<a> self.pattern_file = open(self.rules_filename, encoding='utf-8') <span class=u>&#x2461;</span></a>
def __init__(self):
<a> self.pattern_file = open(self.rules_filename, encoding='utf-8') <span class=u>&#x2460;</span></a>

<a> def __iter__(self): <span class=u>&#x2461;</span></a>
<a> self.cache = [] <span class=u>&#x2462;</span></a>
self.cache_index = 0</code></pre>
<ol>
<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.
<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!
<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.
<li>When we instantiate the <code>LazyRules</code> class, open the pattern file but don&#8217;t read anything from it. (That comes later.)
<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.
<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.
</ol>

<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.
Expand Down
2 changes: 1 addition & 1 deletion table-of-contents.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ <h1>Table of Contents</h1>
<ol>
<li><a href=your-first-python-program.html#docstrings>Docstrings</a>
</ol>
<li><a href=your-first-python-program.html#importsearchpath>The <code>import</code> search path</a>
<li><a href=your-first-python-program.html#everythingisanobject>Everything is an object</a>
<ol>
<li><a href=your-first-python-program.html#importsearchpath>The <code>import</code> search path</a>
<li><a href=your-first-python-program.html#whatsanobject>What&#8217;s an object?</a>
</ol>
<li><a href=your-first-python-program.html#indentingcode>Indenting code</a>
Expand Down

0 comments on commit 6c0a137

Please sign in to comment.