diff --git a/iterators.html b/iterators.html
index 4974cde..0672236 100755
--- a/iterators.html
+++ b/iterators.html
@@ -219,10 +219,10 @@ All three of these class methods, __init__, __iter__,
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()
def __init__(self):
self.pattern_file = open(self.rules_filename, encoding='utf-8') ①
-
- def __iter__(self): ②
- self.cache = [] ③
+ self.cache = [] ②
self.cache_index = 0
LazyRules class, open the pattern file but don’t read anything from it. (That comes later.)
-__iter__() method is going to be called after you instantiate the class, assign it to rules, and call iter(rules) to create the iterator. It would also get called again if you created a new iterator from the same rules object.
-__next__() method) as you read the patterns from the pattern file.
+__next__() method) as you read lines from the pattern file.
Before we continue, let’s take a closer look at rules_filename. It’s not defined within the __iter__() method. In fact, it’s not defined within any method. It’s defined at the class level. It’s a class variable, and although you can access it just like an instance variable (self.rules_filename), it is shared across all instances of the LazyRules class.
@@ -298,13 +295,11 @@ rules = LazyRules()
And now back to our show.
def __iter__(self): ①
- self.cache_index = 0 ②
- return self ③
+ return self ②
__iter__() method will be called every time someone — say, a for loop — calls iter(rules).
-__iter__() method returns self, which signals that this class will take care of returning its own values throughout an iteration.
+__iter__() method needs to do is return an iterator. In this case, it returns self, which signals that this class defines a __next__() method which will take care of returning values throughout the iteration.
def __next__(self): ①