Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 7 ■ MORE ABSTRACTION 151 >>> f = FoodExpert() >>> f.init() >>> map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']) [None, None, None, None, None] The first two lines instantiate FoodExpert and initialize the instance, which is assigned to f. The map call simply uses the method addGoodFood with its self parameter bound to f. Because this method doesn’t return anything, the result is a list filled with None. However, a side effect is that f has been updated: >>> f.goodFood ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'] Let’s use this expert to give us a list of recommendations: >>> menu = ['Filet Mignon', 'Pasta', 'Pizza', 'Eggs', 'Bacon', 'Tomato', 'SPAM'] >>> rec = filter(f.likes, menu) >>> rec ['Eggs', 'Bacon', 'SPAM'] What I did here was simply apply f.likes as a filter to a menu; the dishes the expert didn’t like were simply discarded. But what if you want to find out which of these dishes the expert would prefer? I once again turn to the trusty (if rarely used) reduce: >>> reduce(f.prefers, rec) 'SPAM' This basically works just like the example using reduce with max in Chapter 6 (in the section “reduce”). If I had used a different expert, initialized with different preferences, of course, I’d get completely different results, even though the method definitions would be exactly the same. This is the primary difference between standard functional programming and this quasi-functional programming using bound methods; the methods have access to a state that can be used to “customize” them. ■Note You can pass state along with a function like this by using nested scopes as well, as discussed in the previous chapter. The Class Namespace The following two statements are (more or less) equivalent: def foo(x): return x*x foo = lambda x: x*x
152 CHAPTER 7 ■ MORE ABSTRACTION Both create a function that returns the square of its argument, and both bind the variable foo to that function. The name foo may be defined in the global (module) scope, or it may be local to some function or method. The same thing happens when you define a class; all the code in the class statement is executed in a special namespace—the class namespace. This namespace is accessible later by all members of the class. Not all Python programmers know that class definitions are simply code sections that are executed, but it can be useful information. For example, you aren’t restricted to def statements: >>> class C: print 'Class C being defined...' Class C being defined... >>> Okay, that was a bit silly. But consider the following: class MemberCounter: members = 0 def init(self): MemberCounter.members += 1 >>> m1 = MemberCounter() >>> m1.init() >>> MemberCounter.members 1 >>> m2 = MemberCounter() >>> m2.init() >>> MemberCounter.members 2 In the preceding code, a variable is defined in the class scope, which can be accessed by all the members (instances), in this case to count the number of class members. Note the use of init to initialize all the instances: I’ll automate that in Chapter 9. This class scope variable is accessible from every instance as well, just as methods are: >>> m1.members 2 >>> m2.members 2 What happens when you rebind the members attribute in an instance? >>> m1.members = 'Two' >>> m1.members 'Two' >>> m2.members 2 The new members value has been written into an attribute in m1, shadowing the classwide variable. This mirrors the behavior of local and global variables.
- Page 131 and 132: 100 CHAPTER 5 ■ CONDITIONALS, LOO
- 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: 150 CHAPTER 7 ■ MORE ABSTRACTION
- Page 185 and 186: 154 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
152 CHAPTER 7 ■ MORE ABSTRACTION<br />
Both create a function that returns the square of its argument, and both bind the variable<br />
foo <strong>to</strong> that function. The name foo may be defined in the global (module) scope, or it may be<br />
local <strong>to</strong> some function or method. The same thing happens when you define a class; all the<br />
code in the class statement is executed in a special namespace—the class namespace. This<br />
namespace is accessible later by all members of the class. Not all <strong>Python</strong> programmers know<br />
that class definitions are simply code sections that are executed, but it can be useful information.<br />
For example, you aren’t restricted <strong>to</strong> def statements:<br />
>>> class C:<br />
print 'Class C being defined...'<br />
Class C being defined...<br />
>>><br />
Okay, that was a bit silly. But consider the following:<br />
class MemberCounter:<br />
members = 0<br />
def init(self):<br />
MemberCounter.members += 1<br />
>>> m1 = MemberCounter()<br />
>>> m1.init()<br />
>>> MemberCounter.members<br />
1<br />
>>> m2 = MemberCounter()<br />
>>> m2.init()<br />
>>> MemberCounter.members<br />
2<br />
In the preceding code, a variable is defined in the class scope, which can be accessed by all<br />
the members (instances), in this case <strong>to</strong> count the number of class members. Note the use of<br />
init <strong>to</strong> initialize all the instances: I’ll au<strong>to</strong>mate that in Chapter 9.<br />
This class scope variable is accessible from every instance as well, just as methods are:<br />
>>> m1.members<br />
2<br />
>>> m2.members<br />
2<br />
What happens when you rebind the members attribute in an instance?<br />
>>> m1.members = 'Two'<br />
>>> m1.members<br />
'Two'<br />
>>> m2.members<br />
2<br />
The new members value has been written in<strong>to</strong> an attribute in m1, shadowing the classwide<br />
variable. This mirrors the behavior of local and global variables.