diff --git a/diveintopython3.org b/diveintopython3.org index 764cd96..0dc933d 100755 --- a/diveintopython3.org +++ b/diveintopython3.org @@ -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 diff --git a/examples/plural6.py b/examples/plural6.py index 49ef865..14facad 100755 --- a/examples/plural6.py +++ b/examples/plural6.py @@ -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 diff --git a/installing-python.html b/installing-python.html index 2142f4f..938bfe7 100755 --- a/installing-python.html +++ b/installing-python.html @@ -346,7 +346,7 @@ has the same effect as typing a particular string at the help> prompt.
IDLE, the graphical Python Shell, also includes a Python-aware text editor. You’ll see how to use it in the next chapter. +
IDLE, the graphical Python Shell, also includes a Python-aware text editor. You’ll see how to use it in the next section.
⁂
diff --git a/iterators.html b/iterators.html
index 5a3e4e5..50eb560 100755
--- a/iterators.html
+++ b/iterators.html
@@ -217,8 +217,10 @@ All three of these class methods, __init__, __iter__,
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()
class LazyRules:
rules_filename = 'plural6-rules.txt'
- def __iter__(self): ①
- self.pattern_file = open(self.rules_filename, encoding='utf-8') ②
+ def __init__(self):
+ self.pattern_file = open(self.rules_filename, encoding='utf-8') ①
+
+ def __iter__(self): ②
self.cache = [] ③
self.cache_index = 0
__iter__() method is only going to be called once, after you instantiate the class, assign it to rules, and call iter(rules) to create the iterator.
-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.
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.
diff --git a/table-of-contents.html b/table-of-contents.html
index b80e793..adfed04 100755
--- a/table-of-contents.html
+++ b/table-of-contents.html
@@ -38,9 +38,9 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
import search path