moved pattern_file back to __init__ in plural6.py

This commit is contained in:
Mark Pilgrim
2009-07-27 06:20:29 -04:00
parent 62aa447daf
commit 6c0a137742
5 changed files with 16 additions and 10 deletions
+1 -1
View File
@@ -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
+3 -1
View File
@@ -17,8 +17,10 @@ def build_match_and_apply_functions(pattern, search, replace):
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
+1 -1
View File
@@ -346,7 +346,7 @@ has the same effect as typing a particular string at the help> prompt.</samp>
<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;
+10 -6
View File
@@ -217,8 +217,10 @@ All three of these class methods, <code>__init__</code>, <code>__iter__</code>,
<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
@@ -251,14 +253,16 @@ rules = LazyRules()</code></pre>
<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.
+1 -1
View File
@@ -38,9 +38,9 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
<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>