From 34bbe9e9d64859c62070830612123ec82a718340 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Tue, 14 Jul 2009 00:28:46 -0400 Subject: [PATCH] open file as late as possible (in __iter__, not __init__) --- examples/plural6.py | 4 +--- iterators.html | 15 +++++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) mode change 100644 => 100755 examples/plural6.py diff --git a/examples/plural6.py b/examples/plural6.py old mode 100644 new mode 100755 index f29293b..945803f --- a/examples/plural6.py +++ b/examples/plural6.py @@ -17,11 +17,9 @@ def build_match_and_apply_functions(pattern, search, replace): class LazyRules: rules_filename = 'plural6-rules.txt' - def __init__(self): + def __iter__(self): self.pattern_file = open(self.rules_filename) self.cache = [] - - def __iter__(self): self.cache_index = 0 return self diff --git a/iterators.html b/iterators.html index 573c47b..81b762d 100755 --- a/iterators.html +++ b/iterators.html @@ -217,11 +217,9 @@ All three of these class methods, __init__, __iter__,
class LazyRules:
     rules_filename = 'plural6-rules.txt'
 
-    def __init__(self):
+    def __iter__(self):
         self.pattern_file = open(self.rules_filename)
         self.cache = []
-
-    def __iter__(self):
         self.cache_index = 0
         return self
 
@@ -253,16 +251,17 @@ rules = LazyRules()
class LazyRules:
     rules_filename = 'plural6-rules.txt'
 
-    def __init__(self):                                
+    def __iter__(self):                                
         self.pattern_file = open(self.rules_filename)  
-        self.cache = []                                
+ self.cache = [] + self.cache_index = 0
    -
  1. The __init__() method is only going to be called once, when you instantiate the class and assign it to rules. -
  2. Since this is only going to get called once, it’s the perfect place to open the pattern file. You’ll read it later; no point doing more than you absolutely have to until absolutely necessary! +
  3. 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. +
  4. 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!
  5. Also, this is a good place to initialize the cache, which you’ll use later 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 __init__() 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. +

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.

 >>> import plural6