mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
fixed plural6 for the last fucking time
This commit is contained in:
+6
-11
@@ -219,10 +219,10 @@ All three of these class methods, <code>__init__</code>, <code>__iter__</code>,
|
||||
|
||||
def __init__(self):
|
||||
self.pattern_file = open(self.rules_filename, encoding='utf-8')
|
||||
|
||||
def __iter__(self):
|
||||
self.cache = []
|
||||
self.cache_index = 0
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
@@ -255,14 +255,11 @@ rules = LazyRules()</code></pre>
|
||||
|
||||
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>
|
||||
<a> self.cache = [] <span class=u>②</span></a>
|
||||
self.cache_index = 0</code></pre>
|
||||
<ol>
|
||||
<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.
|
||||
<li>After opening the patterns file, initialize the cache and the cache index position. You’ll use these later (in the <code>__next__()</code> method) as you read lines 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.
|
||||
@@ -298,13 +295,11 @@ rules = LazyRules()</code></pre>
|
||||
<p>And now back to our show.
|
||||
|
||||
<pre class=pp><code><a> def __iter__(self): <span class=u>①</span></a>
|
||||
<a> self.cache_index = 0 <span class=u>②</span></a>
|
||||
<a> return self <span class=u>③</span></a>
|
||||
<a> return self <span class=u>②</span></a>
|
||||
</code></pre>
|
||||
<ol>
|
||||
<li>The <code>__iter__()</code> method will be called every time someone — say, a <code>for</code> loop — calls <code>iter(rules)</code>.
|
||||
<li>This is the place to reset the counter that we’re going to use to retrieve items from the cache (that we haven’t built yet — patience, grasshopper).
|
||||
<li>Finally, the <code>__iter__()</code> method returns <var>self</var>, which signals that this class will take care of returning its own values throughout an iteration.
|
||||
<li>The only thing the <code>__iter__()</code> method needs to do is return an iterator. In this case, it returns <var>self</var>, which signals that this class defines a <code>__next__()</code> method which will take care of returning values throughout the iteration.
|
||||
</ol>
|
||||
|
||||
<pre class=pp><code><a> def __next__(self): <span class=u>①</span></a>
|
||||
|
||||
Reference in New Issue
Block a user