mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
open file as late as possible (in __iter__, not __init__)
This commit is contained in:
Regular → Executable
+1
-3
@@ -17,11 +17,9 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
class LazyRules:
|
||||
rules_filename = 'plural6-rules.txt'
|
||||
|
||||
def __init__(self):
|
||||
def __iter__(self):
|
||||
self.pattern_file = open(self.rules_filename)
|
||||
self.cache = []
|
||||
|
||||
def __iter__(self):
|
||||
self.cache_index = 0
|
||||
return self
|
||||
|
||||
|
||||
+7
-8
@@ -217,11 +217,9 @@ All three of these class methods, <code>__init__</code>, <code>__iter__</code>,
|
||||
<pre><code class=pp>class LazyRules:
|
||||
rules_filename = 'plural6-rules.txt'
|
||||
|
||||
def __init__(self):
|
||||
def __iter__(self):
|
||||
self.pattern_file = open(self.rules_filename)
|
||||
self.cache = []
|
||||
|
||||
def __iter__(self):
|
||||
self.cache_index = 0
|
||||
return self
|
||||
|
||||
@@ -253,16 +251,17 @@ rules = LazyRules()</code></pre>
|
||||
<pre><code class=pp>class LazyRules:
|
||||
rules_filename = 'plural6-rules.txt'
|
||||
|
||||
<a> def __init__(self): <span class=u>①</span></a>
|
||||
<a> def __iter__(self): <span class=u>①</span></a>
|
||||
<a> self.pattern_file = open(self.rules_filename) <span class=u>③</span></a>
|
||||
<a> self.cache = [] <span class=u>②</span></a></code></pre>
|
||||
<a> self.cache = [] <span class=u>②</span></a>
|
||||
self.cache_index = 0</code></pre>
|
||||
<ol>
|
||||
<li>The <code>__init__()</code> method is only going to be called once, when you instantiate the class and assign it to <var>rules</var>.
|
||||
<li>Since this is only going to get called once, it’s the perfect place to open the pattern file. You’ll read it later; no point doing more than you absolutely have to until absolutely necessary!
|
||||
<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.
|
||||
</ol>
|
||||
|
||||
<p>Before we continue, let’s take a closer look at <var>rules_filename</var>. It’s not defined within the <code>__init__()</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.
|
||||
<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.
|
||||
|
||||
<pre class=screen>
|
||||
<samp class=p>>>> </samp><kbd class=pp>import plural6</kbd>
|
||||
|
||||
Reference in New Issue
Block a user