You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<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.
260
-
<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!
261
-
<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.
263
+
<li>When we instantiate the <code>LazyRules</code>class, open the pattern file but don’t read anything from it. (That comes later.)
264
+
<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.
265
+
<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.
262
266
</ol>
263
267
264
268
<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.
0 commit comments