Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional

16.01.2014 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!