Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 6 ■ ■ ■ Abstraction In this chapter, you learn how to group statements into functions, which enables you to tell the computer how to do something, and to tell it only once. You won’t have to give it the same detailed instructions over and over. The chapter provides a thorough introduction to parameters and scoping; you learn what recursion is and what it can do for your programs, and you see how functions themselves can be used as parameters, just like numbers, strings, and other objects. Laziness Is a Virtue The programs we’ve written so far have been pretty small, but if you want to make something bigger, you’ll soon run into trouble. Consider what happens if you have written some code in one place and need to use it in another place as well. For example, let’s say you wrote a snippet of code that computed some Fibonacci numbers (a series of numbers in which each number is the sum of the two previous ones): fibs = [0, 1] for i in range(8): fibs.append(fibs[-2] + fibs[-1]) After running this, fibs contains the first ten Fibonacci numbers: >>> fibs [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] This is all right if what you want is to calculate the first ten Fibonacci numbers once. You could even change the for loop to work with a dynamic range, with the length of the resulting sequence supplied by the user: fibs = [0, 1] num = input('How many Fibonacci numbers do you want? ') for i in range(num-2): fibs.append(fibs[-2] + fibs[-1]) print fibs ■Note Remember that you can use raw_input if you want to read in a plain string. In this case, you would then have had to convert it to an integer by using the int function. 109
110 CHAPTER 6 ■ ABSTRACTION But what if you also want to use the numbers for something else? You could certainly just write the same loop again when needed, but what if you had written a more complicated piece of code, for example, one that downloaded a set of Web pages and computed the frequencies of all the words used? Would you still want to write all the code several times, once for each time you needed it? No, real programmers don’t do that. Real programmers are lazy. Not lazy in a bad way, but in the sense that they don’t do unnecessary work. So what do real programmers do? They make their programs more abstract. You could make the previous program more abstract as follows: num = input('How many numbers do you want? ') print fibs(num) Here, only what is specific to this program is written concretely (reading in the number, and printing out the result). Actually computing the Fibonacci numbers is done in an abstract manner: you simply tell the computer to do it. You don’t say specifically how it should be done. You create a function called fibs, and use it when you need the functionality of the little Fibonacci program. It saves you a lot of effort if you need it in several places. Abstraction and Structure Abstraction can be useful as a labor saver, but it is actually more important than that. It is the key to making computer programs understandable to humans (which is essential, whether you’re writing them or reading them). The computers themselves are perfectly happy with very concrete and specific instructions, but humans generally aren’t. If you ask me for directions to the cinema, for example, you wouldn’t want me to answer, “Walk 10 steps forward, turn 90 degrees to your left, walk another 5 steps, turn 45 degrees to your right, walk 123 steps.” You would soon lose track, wouldn’t you? Now, if I instead told you to “Walk down this street until you get to a bridge, cross the bridge, and the cinema is to your left,” then you’d certainly understand me. The point is that you already know how to walk down the street, and how to cross a bridge. You don’t need explicit instructions on how to do either. You structure computer programs in a similar fashion. Your programs should be quite abstract, as in “Download page, compute frequencies, print the frequency of each word.” This is easily understandable. In fact, let’s translate this high-level description to a Python program right now: page = download_page() freqs = compute_frequencies(page) for word, freq in freqs: print word, freq From reading this, you can understand what the program does. However, you haven’t explicitly said anything about how it should do it. You just tell the computer to download the page and compute the frequencies. The specifics of these operations will have to be written somewhere else—in separate function definitions.
- Page 90 and 91: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 94 and 95: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 96 and 97: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 98 and 99: CHAPTER 4 ■ ■ ■ Dictionaries:
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 102 and 103: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 108 and 109: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 110: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 113 and 114: 82 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 115 and 116: 84 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 117 and 118: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 119 and 120: 88 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 121 and 122: 90 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 123 and 124: 92 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 125 and 126: 94 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 127 and 128: 96 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 129 and 130: 98 CHAPTER 5 ■ CONDITIONALS, LOOP
- 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: 108 CHAPTER 5 ■ CONDITIONALS, LOO
- 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 and 184: 152 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
110 CHAPTER 6 ■ ABSTRACTION<br />
But what if you also want <strong>to</strong> use the numbers for something else? You could certainly just<br />
write the same loop again when needed, but what if you had written a more complicated piece<br />
of code, for example, one that downloaded a set of Web pages and computed the frequencies<br />
of all the words used? Would you still want <strong>to</strong> write all the code several times, once for each<br />
time you needed it? No, real programmers don’t do that. Real programmers are lazy. Not lazy<br />
in a bad way, but in the sense that they don’t do unnecessary work.<br />
So what do real programmers do? They make their programs more abstract. You could<br />
make the previous program more abstract as follows:<br />
num = input('How many numbers do you want? ')<br />
print fibs(num)<br />
Here, only what is specific <strong>to</strong> this program is written concretely (reading in the number, and<br />
printing out the result). Actually computing the Fibonacci numbers is done in an abstract manner:<br />
you simply tell the computer <strong>to</strong> do it. You don’t say specifically how it should be done. You create a<br />
function called fibs, and use it when you need the functionality of the little Fibonacci program.<br />
It saves you a lot of effort if you need it in several places.<br />
Abstraction and Structure<br />
Abstraction can be useful as a labor saver, but it is actually more important than that. It is the<br />
key <strong>to</strong> making computer programs understandable <strong>to</strong> humans (which is essential, whether<br />
you’re writing them or reading them). The computers themselves are perfectly happy with very<br />
concrete and specific instructions, but humans generally aren’t. If you ask me for directions <strong>to</strong><br />
the cinema, for example, you wouldn’t want me <strong>to</strong> answer, “Walk 10 steps forward, turn 90 degrees<br />
<strong>to</strong> your left, walk another 5 steps, turn 45 degrees <strong>to</strong> your right, walk 123 steps.” You would soon<br />
lose track, wouldn’t you?<br />
Now, if I instead <strong>to</strong>ld you <strong>to</strong> “Walk down this street until you get <strong>to</strong> a bridge, cross the<br />
bridge, and the cinema is <strong>to</strong> your left,” then you’d certainly understand me. The point is that<br />
you already know how <strong>to</strong> walk down the street, and how <strong>to</strong> cross a bridge. You don’t need<br />
explicit instructions on how <strong>to</strong> do either.<br />
You structure computer programs in a similar fashion. Your programs should be quite<br />
abstract, as in “Download page, compute frequencies, print the frequency of each word.” This<br />
is easily understandable. In fact, let’s translate this high-level description <strong>to</strong> a <strong>Python</strong> program<br />
right now:<br />
page = download_page()<br />
freqs = compute_frequencies(page)<br />
for word, freq in freqs:<br />
print word, freq<br />
<strong>From</strong> reading this, you can understand what the program does. However, you haven’t<br />
explicitly said anything about how it should do it. You just tell the computer <strong>to</strong> download the<br />
page and compute the frequencies. The specifics of these operations will have <strong>to</strong> be written<br />
somewhere else—in separate function definitions.