diff --git a/iterators.html b/iterators.html index 9ccdf63..8e8ac5a 100644 --- a/iterators.html +++ b/iterators.html @@ -101,7 +101,7 @@ class Fib:
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.
Fib class.
-__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__.
+__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.
docstring just as with a function or a module. All instances of a class share the same docstring.
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 = [] ②
__init__() method is only going to be called once, when you instantiate the class and assign it to rules.
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'