From 3e357ea4beaa3d3b38e58f6ecffc29fd60f751e1 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sat, 16 May 2009 01:21:46 -0400 Subject: [PATCH] various minor errors in iterators chapter [thanks G.P.] --- iterators.html | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/iterators.html b/iterators.html index 9ccdf63..8e8ac5a 100644 --- a/iterators.html +++ b/iterators.html @@ -101,7 +101,7 @@ class Fib:
  1. You are creating an instance of the Fib class (defined in the fibonacci2 module) and assigning the newly created instance to the variable fib. You are passing one parameter, 100, which will end up as the max argument in Fib’s __init__() method.
  2. fib is now an instance of the Fib class. -
  3. Every class instance has a built-in attribute, __class__, which is the object’s class. Java programmers may be familiar with the Class class, which contains methods like getName and getSuperclass to get metadata information about an object. In Python, this kind of metadata is available directly on the object itself through attributes like __class__, __name__, and __bases__. +
  4. Every class instance has a built-in attribute, __class__, which is the object’s class. Java programmers may be familiar with the Class class, which contains methods like getName() and getSuperclass() to get metadata information about an object. In Python, this kind of metadata is available through attributes, but the idea is the same.
  5. You can access the instance’s docstring just as with a function or a module. All instances of a class share the same docstring.
@@ -235,33 +235,33 @@ rules = LazyRules()

Let’s take the class one bite at a time.

class LazyRules:
-    rules_f = 'plural6-rules.txt'
+    rules_filename = 'plural6-rules.txt'
 
-    def __init__(self):                         
-        self.pattern_file = open(self.rules_f)  
-        self.cache = []                         
+ def __init__(self): + self.pattern_file = open(self.rules_filename) + self.cache = []
  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. 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_f. 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_f), 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 __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.

 >>> import plural6
 >>> r1 = plural6.LazyRules()
 >>> r2 = plural6.LazyRules()
->>> r1.rules_f                               
+>>> r1.rules_filename                               
 'plural6-rules.txt'
->>> r2.rules_f
+>>> r2.rules_filename
 'plural6-rules.txt'
->>> r1.__class__.rules_f                     
+>>> r1.__class__.rules_filename                     
 'plural6-rules.txt'
->>> r1.__class__.rules_f = 'papayawhip.txt'  
->>> r1.rules_f
+>>> r1.__class__.rules_filename = 'papayawhip.txt'  
+>>> r1.rules_filename
 'papayawhip.txt'
->>> r2.rules_f                               
+>>> r2.rules_filename                               
 'papayawhip.txt'
  1. FIXME