mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
moved pattern_file back to __init__ in plural6.py
This commit is contained in:
+1
-1
@@ -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
@@ -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
|
||||
|
||||
@@ -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’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’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’ll see how to use it in the next section.
|
||||
|
||||
<p class=a>⁂
|
||||
|
||||
|
||||
+10
-6
@@ -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>①</span></a>
|
||||
<a> self.pattern_file = open(self.rules_filename, encoding='utf-8') <span class=u>②</span></a>
|
||||
def __init__(self):
|
||||
<a> self.pattern_file = open(self.rules_filename, encoding='utf-8') <span class=u>①</span></a>
|
||||
|
||||
<a> def __iter__(self): <span class=u>②</span></a>
|
||||
<a> self.cache = [] <span class=u>③</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’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’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’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’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’s take a closer look at <var>rules_filename</var>. It’s not defined within the <code>__iter__()</code> method. In fact, it’s not defined within <em>any</em> method. It’s defined at the class level. It’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.
|
||||
|
||||
@@ -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’s an object?</a>
|
||||
</ol>
|
||||
<li><a href=your-first-python-program.html#indentingcode>Indenting code</a>
|
||||
|
||||
Reference in New Issue
Block a user