Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 7 ■ MORE ABSTRACTION 153 Specifying a Superclass As I discussed earlier in the chapter, subclasses expand on the definitions in their superclasses. You indicate the superclass in a class statement by writing it in parentheses after the class name: class Filter: def init(self): self.blocked = [] def filter(self, sequence): return [x for x in sequence if x not in self.blocked] class SPAMFilter(Filter): # SPAMFilter is a subclass of Filter def init(self): # Overrides init method from Filter superclass self.blocked = ['SPAM'] Filter is a general class for filtering sequences. Actually it doesn’t filter out anything: >>> f = Filter() >>> f.init() >>> f.filter([1, 2, 3]) [1, 2, 3] The usefulness of the Filter class is that it can be used as a base class (superclass) for other classes, such as SPAMFilter, which filters out 'SPAM' from sequences: >>> s = SPAMFilter() >>> s.init() >>> s.filter(['SPAM', 'SPAM', 'SPAM', 'SPAM', 'eggs', 'bacon', 'SPAM']) ['eggs', 'bacon'] Note two important points in the definition of SPAMFilter: • I override the definition of init from Filter by simply providing a new definition. • The definition of the filter method carries over (is inherited) from Filter, so you don’t have to write the definition again. The second point demonstrates why inheritance is useful: I can now make a number of different filter classes, all subclassing Filter, and for each one I can simply use the filter method I have already implemented. Talk about useful laziness... Investigating Inheritance If you want to find out whether a class is a subclass of another, you can use the built-in method issubclass: >>> issubclass(SPAMFilter, Filter) True >>> issubclass(Filter, SPAMFilter) False
154 CHAPTER 7 ■ MORE ABSTRACTION If you have a class and want to know its base classes, you can access its special attribute __bases__: >>> SPAMFilter.__bases__ (,) >>> Filter.__bases__ () In a similar manner, you can check whether an object is an instance of a class by using isinstance: >>> s = SPAMFilter() >>> isinstance(s, SPAMFilter) True >>> isinstance(s, Filter) True >>> isinstance(s, str) False ■Tip As mentioned before, isinstance is best left unused most of the time. Relying on polymorphism is almost always better. As you can see, s is a (direct) member of the class SPAMFilter, but it is also an indirect member of Filter because SPAMFilter is a subclass of Filter. Another way of putting it is that all SPAMFilters are Filters. As you can see in the last example, isinstance also works with types, such as the string type (str). If you just want to find out which class an object belongs to, you can use the __class__ attribute: >>> s.__class__ Multiple Superclasses I’m sure you noticed a small detail in the previous section that may have seemed odd: the plural form in __bases__. I said you could use it to find the base classes of a class, which implies that it may have more than one. This is, in fact, the case. To show how it works, let’s create a few classes: class Calculator: def calculate(self, expression): self.value = eval(expression) class Talker: def talk(self): print 'Hi, my value is', self.value
- Page 133 and 134: 102 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 135 and 136: 104 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 137 and 138: 106 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 139 and 140: 108 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 141 and 142: 110 CHAPTER 6 ■ ABSTRACTION But w
- Page 143 and 144: 112 CHAPTER 6 ■ ABSTRACTION ■Ti
- Page 145 and 146: 114 CHAPTER 6 ■ ABSTRACTION Can I
- Page 147 and 148: 116 CHAPTER 6 ■ ABSTRACTION stora
- Page 149 and 150: 118 CHAPTER 6 ■ ABSTRACTION 4. Yo
- Page 151 and 152: 120 CHAPTER 6 ■ ABSTRACTION The p
- Page 153 and 154: 122 CHAPTER 6 ■ ABSTRACTION >>> p
- Page 155 and 156: 124 CHAPTER 6 ■ ABSTRACTION Also,
- Page 157 and 158: 126 CHAPTER 6 ■ ABSTRACTION Feel
- Page 159 and 160: 128 CHAPTER 6 ■ ABSTRACTION ■No
- Page 161 and 162: 130 CHAPTER 6 ■ ABSTRACTION So yo
- Page 163 and 164: 132 CHAPTER 6 ■ ABSTRACTION numer
- Page 165 and 166: 134 CHAPTER 6 ■ ABSTRACTION LAMBD
- Page 167 and 168: 136 CHAPTER 6 ■ ABSTRACTION In th
- Page 169 and 170: 138 CHAPTER 6 ■ ABSTRACTION Scope
- Page 171 and 172: 140 CHAPTER 7 ■ MORE ABSTRACTION
- Page 173 and 174: 142 CHAPTER 7 ■ MORE ABSTRACTION
- Page 175 and 176: 144 CHAPTER 7 ■ MORE ABSTRACTION
- Page 177 and 178: 146 CHAPTER 7 ■ MORE ABSTRACTION
- Page 179 and 180: 148 CHAPTER 7 ■ MORE ABSTRACTION
- Page 181 and 182: 150 CHAPTER 7 ■ MORE ABSTRACTION
- Page 183: 152 CHAPTER 7 ■ MORE ABSTRACTION
- Page 187 and 188: 156 CHAPTER 7 ■ MORE ABSTRACTION
- Page 189 and 190: 158 CHAPTER 7 ■ MORE ABSTRACTION
- Page 191 and 192: 160 CHAPTER 8 ■ EXCEPTIONS >>> im
- Page 193 and 194: 162 CHAPTER 8 ■ EXCEPTIONS Custom
- Page 195 and 196: 164 CHAPTER 8 ■ EXCEPTIONS More T
- Page 197 and 198: 166 CHAPTER 8 ■ EXCEPTIONS try: x
- Page 199 and 200: 168 CHAPTER 8 ■ EXCEPTIONS Invali
- Page 201 and 202: 170 CHAPTER 8 ■ EXCEPTIONS If you
- Page 204 and 205: CHAPTER 9 ■ ■ ■ Magic Methods
- Page 206 and 207: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 208 and 209: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 210 and 211: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 212 and 213: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 214 and 215: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 216 and 217: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 218 and 219: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 220 and 221: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 222 and 223: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 224 and 225: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 226 and 227: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 228 and 229: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 230 and 231: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 232 and 233: CHAPTER 9 ■ MAGIC METHODS, PROPER
154 CHAPTER 7 ■ MORE ABSTRACTION<br />
If you have a class and want <strong>to</strong> know its base classes, you can access its special attribute<br />
__bases__:<br />
>>> SPAMFilter.__bases__<br />
(,)<br />
>>> Filter.__bases__<br />
()<br />
In a similar manner, you can check whether an object is an instance of a class by using<br />
isinstance:<br />
>>> s = SPAMFilter()<br />
>>> isinstance(s, SPAMFilter)<br />
True<br />
>>> isinstance(s, Filter)<br />
True<br />
>>> isinstance(s, str)<br />
False<br />
■Tip As mentioned before, isinstance is best left unused most of the time. Relying on polymorphism is<br />
almost always better.<br />
As you can see, s is a (direct) member of the class SPAMFilter, but it is also an indirect<br />
member of Filter because SPAMFilter is a subclass of Filter. Another way of putting it is that<br />
all SPAMFilters are Filters. As you can see in the last example, isinstance also works with types,<br />
such as the string type (str).<br />
If you just want <strong>to</strong> find out which class an object belongs <strong>to</strong>, you can use the __class__<br />
attribute:<br />
>>> s.__class__<br />
<br />
Multiple Superclasses<br />
I’m sure you noticed a small detail in the previous section that may have seemed odd: the<br />
plural form in __bases__. I said you could use it <strong>to</strong> find the base classes of a class, which implies<br />
that it may have more than one. This is, in fact, the case. To show how it works, let’s create a few<br />
classes:<br />
class Calcula<strong>to</strong>r:<br />
def calculate(self, expression):<br />
self.value = eval(expression)<br />
class Talker:<br />
def talk(self):<br />
print 'Hi, my value is', self.value