From 6c0a13774227816c926f2e691054d3d91112e23c Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Mon, 27 Jul 2009 06:20:29 -0400 Subject: [PATCH] moved pattern_file back to __init__ in plural6.py --- diveintopython3.org | 2 +- examples/plural6.py | 4 +++- installing-python.html | 2 +- iterators.html | 16 ++++++++++------ table-of-contents.html | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) 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.
  • The prompt changes back to >>> to signal that you’ve left the interactive help mode and returned to the Python Shell. -

    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
      -
    1. The __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. -
    2. 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! -
    3. Also, this is a good place to initialize the cache, which you’ll use later as you read the patterns from the pattern file. +
    4. When we instantiate the LazyRules class, open the pattern file but don’t read anything from it. (That comes later.) +
    5. The __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. +
    6. That means that this is a right place to initialize the cache and the cache index position. You’ll use these later (in the __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}

    1. Docstrings
    +
  • The import search path
  • Everything is an object
      -
    1. The import search path
    2. What’s an object?
  • Indenting code