Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 9 ■ MAGIC METHODS, PROPERTIES, AND ITERATORS 181 Initialize the arithmetic sequence. start - the first value in the sequence step - the difference between two adjacent values changed - a dictionary of values that have been modified by the user """ self.start = start # Store the start value self.step = step # Store the step value self.changed = {} # No items have been modified def __getitem__(self, key): """ Get an item from the arithmetic sequence. """ checkIndex(key) try: return self.changed[key] except KeyError: return self.start + key*self.step # Modified? # otherwise... # ...calculate the value def __setitem__(self, key, value): """ Change an item in the arithmetic sequence. """ checkIndex(key) self.changed[key] = value # Store the changed value This implements an arithmetic sequence, a sequence of numbers in which each is greater than the previous one by a constant amount. The first value is given by the constructor parameter start (defaulting to zero), while the step between the values is given by step (defaulting to one). You allow the user to change some of the elements by storing the exceptions to the general rule in a dictionary called changed. If the element hasn’t been changed, it is calculated as start+key*step. Here is an example of how you can use this class: >>> s = ArithmeticSequence(1, 2) >>> s[4] 9 >>> s[4] = 2 >>> s[4] 2 >>> s[5] 11 Note that it is illegal to delete items, which is why I haven’t implemented __del__:
182 CHAPTER 9 ■ MAGIC METHODS, PROPERTIES, AND ITERATORS >>> del s[4] Traceback (most recent call last): File "", line 1, in ? AttributeError: ArithmeticSequence instance has no attribute '__delitem__' Also, the class has no __len__ method because it is of infinite length. If an illegal type of index is used, a TypeError is raised, and if the index is the correct type but out of range (negative in the last of the following two examples), an IndexError is raised: >>> s["four"] Traceback (most recent call last): File "", line 1, in ? File "arithseq.py", line 31, in __getitem__ checkIndex(key) File "arithseq.py", line 10, in checkIndex if not isinstance(key, int): raise TypeError TypeError >>> s[-42] Traceback (most recent call last): File "", line 1, in ? File "arithseq.py", line 31, in __getitem__ checkIndex(key) File "arithseq.py", line 11, in checkIndex if key
- 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
- 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 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
- Page 234 and 235: CHAPTER 10 ■ ■ ■ Batteries In
- Page 236 and 237: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 238 and 239: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 240 and 241: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 242 and 243: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 244 and 245: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 246 and 247: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 248 and 249: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 250 and 251: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 252 and 253: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 254 and 255: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 256 and 257: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 258 and 259: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 260 and 261: CHAPTER 10 ■ BATTERIES INCLUDED 2
182 CHAPTER 9 ■ MAGIC METHODS, PROPERTIES, AND ITERATORS<br />
>>> del s[4]<br />
Traceback (most recent call last):<br />
File "", line 1, in ?<br />
AttributeError: ArithmeticSequence instance has no attribute '__delitem__'<br />
Also, the class has no __len__ method because it is of infinite length.<br />
If an illegal type of index is used, a TypeError is raised, and if the index is the correct type<br />
but out of range (negative in the last of the following two examples), an IndexError is raised:<br />
>>> s["four"]<br />
Traceback (most recent call last):<br />
File "", line 1, in ?<br />
File "arithseq.py", line 31, in __getitem__<br />
checkIndex(key)<br />
File "arithseq.py", line 10, in checkIndex<br />
if not isinstance(key, int): raise TypeError<br />
TypeError<br />
>>> s[-42]<br />
Traceback (most recent call last):<br />
File "", line 1, in ?<br />
File "arithseq.py", line 31, in __getitem__<br />
checkIndex(key)<br />
File "arithseq.py", line 11, in checkIndex<br />
if key