Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 6 ■ ABSTRACTION 125 print 'Received redundant parameters:', others return pow(x, y) def interval(start, stop=None, step=1): 'Imitates range() for step > 0' if stop is None: # If the stop is not supplied... start, stop = 0, start # shuffle the parameters result = [] i = start # We start counting at the start index while i < stop: # Until the index reaches the stop index... result.append(i) # ...append the index to the result... i += step # ...increment the index with the step (> 0) return result Now let’s try them out: >>> print story(job='king', name='Gumby') Once upon a time, there was a king called Gumby. >>> print story(name='Sir Robin', job='brave knight') Once upon a time, there was a brave knight called Sir Robin. >>> params = {'job': 'language', 'name': 'Python'} >>> print story(**params) Once upon a time, there was a language called Python. >>> del params['job'] >>> print story(job='stroke of genius', **params) Once upon a time, there was a stroke of genius called Python. >>> power(2,3) 8 >>> power(3,2) 9 >>> power(y=3,x=2) 8 >>> params = (5,) * 2 >>> power(*params) 3125 >>> power(3, 3, 'Hello, world') Received redundant parameters: ('Hello, world',) 27 >>> interval(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> interval(1,5) [1, 2, 3, 4] >>> interval(3,12,4) [3, 7, 11] >>> power(*interval(3,7)) Received redundant parameters: (5, 6) 81
126 CHAPTER 6 ■ ABSTRACTION Feel free to experiment with these functions and functions of your own until you are confident that you understand how this stuff works. Scoping What are variables, really? You can think of them as names referring to values. So, after the assignment x = 1, the name x refers to the value 1. It’s almost like using dictionaries, where keys refer to values, except that you’re using an “invisible” dictionary. Actually, this isn’t far from the truth. There is a built-in function called vars, which returns this dictionary: >>> x = 1 >>> scope = vars() >>> scope['x'] 1 >>> scope['x'] += 1 >>> x 2 ■Caution In general, you should not modify the dictionary returned by vars because, according to the official Python documentation, the result is undefined. In other words, you might not get the result you’re after. This sort of “invisible dictionary” is called a namespace or scope. So, how many namespaces are there? In addition to the global scope, each function call creates a new one: >>> def foo(): x = 42 ... >>> x = 1 >>> foo() >>> x 1 Here foo changes (rebinds) the variable x, but when you look at it in the end, it hasn’t changed after all. That’s because when you call foo a new namespace is created, which is used for the block inside foo. The assignment x = 42 is performed in this inner scope (the local namespace), and therefore it doesn’t affect the x in the outer (global) scope. Variables that are used inside functions like this are called local variables (as opposed to global variables). The parameters work just like local variables, so there is no problem in having a parameter with the same name as a global variable: >>> def output(x): print x ... >>> x = 1 >>> y = 2 >>> output(y) 2
- 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 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: 124 CHAPTER 6 ■ ABSTRACTION Also,
- 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
- 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
CHAPTER 6 ■ ABSTRACTION 125<br />
print 'Received redundant parameters:', others<br />
return pow(x, y)<br />
def interval(start, s<strong>to</strong>p=None, step=1):<br />
'Imitates range() for step > 0'<br />
if s<strong>to</strong>p is None:<br />
# If the s<strong>to</strong>p is not supplied...<br />
start, s<strong>to</strong>p = 0, start # shuffle the parameters<br />
result = []<br />
i = start<br />
# We start counting at the start index<br />
while i < s<strong>to</strong>p:<br />
# Until the index reaches the s<strong>to</strong>p index...<br />
result.append(i) # ...append the index <strong>to</strong> the result...<br />
i += step # ...increment the index with the step (> 0)<br />
return result<br />
Now let’s try them out:<br />
>>> print s<strong>to</strong>ry(job='king', name='Gumby')<br />
Once upon a time, there was a king called Gumby.<br />
>>> print s<strong>to</strong>ry(name='Sir Robin', job='brave knight')<br />
Once upon a time, there was a brave knight called Sir Robin.<br />
>>> params = {'job': 'language', 'name': '<strong>Python</strong>'}<br />
>>> print s<strong>to</strong>ry(**params)<br />
Once upon a time, there was a language called <strong>Python</strong>.<br />
>>> del params['job']<br />
>>> print s<strong>to</strong>ry(job='stroke of genius', **params)<br />
Once upon a time, there was a stroke of genius called <strong>Python</strong>.<br />
>>> power(2,3)<br />
8<br />
>>> power(3,2)<br />
9<br />
>>> power(y=3,x=2)<br />
8<br />
>>> params = (5,) * 2<br />
>>> power(*params)<br />
3125<br />
>>> power(3, 3, 'Hello, world')<br />
Received redundant parameters: ('Hello, world',)<br />
27<br />
>>> interval(10)<br />
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br />
>>> interval(1,5)<br />
[1, 2, 3, 4]<br />
>>> interval(3,12,4)<br />
[3, 7, 11]<br />
>>> power(*interval(3,7))<br />
Received redundant parameters: (5, 6)<br />
81