The definition for class SimpleDictionary in Fig. 8.15 does not include all the methods suggested for providing a dictionary interface. Review the list of mapping methods in Fig. 8.14, and modify the definition for class SimpleDictionary to include definitions for methods clear, copy, get, has_key and update. Each method of class SimpleDictionary should call attribute __dict__’s corresponding
method, passing any necessary arguments. Review the de- scription of dictionary methods in Section 5.6—the corresponding methods of class SimpleDic- tionary should specify the same arguments and should return the same value.
What will be an ideal response?
```
# Definition of class SimpleDictionary with all dictionary methods.
class SimpleDictionary:
"""Class to make an instance behave like a dictionary"""
# add this constructor so method copy can return an object
# of class SimpleDictionary
def __init__( self, initialDictionary = None ):
"""SimpleDictionary constructor,
takes initial dictionary"""
if initialDictionary:
for key, value in initialDictionary.items():
self[ key ] = value
# mapping special methods
def __getitem__( self, key ):
"""Overloaded key-value access"""
return self.__dict__[ key ]
def __setitem__( self, key, value ):
"""Overloaded key-value assignment/creation"""
self.__dict__[ key ] = value
def __delitem__( self, key ):
"""Overloaded key-value deletion"""
del self.__dict__[ key ]
def __str__( self ):
"""Overloaded string representation"""
return str( self.__dict__ )
def __contains__( self, key ):
"""Overloaded membership test (in)"""
return key in self.__dict__
def __len__( self ):
"""Returns length of dictionary"""
return len( self.__dict__ )
# common mapping methods
def keys( self ):
"""Returns list of keys in dictionary"""
return self.__dict__.keys()
def values( self ):
"""Returns list of values in dictionary"""
return self.__dict__.values()
def items( self ):
"""Returns list of items in dictionary"""
return self.__dict__.items()
def clear( self ):
"""Clears dictionary"""
return self.__dict__.clear()
def copy( self ):
"""Returns shallow copy of instance’s __dict__"""
return SimpleDictionary( self.__dict__.copy() )
def get( self, key ):
"""Returns value of key"""
return self.__dict__[ key ]
def has_key( self, key ):
"""Returns 1 if key is in dictionary, 0 otherwise"""
return self.__dict__.has_key( key )
def update( self, other ):
"""Updates dictionary with another dictionary"""
return self.__dict__.update( other )
```
>>> simple = SimpleDictionary()
>>> simple[ 1 ] = "one"
>>> simple[ 2 ] = "two"
>>> simple[ 3 ] = "three"
>>> print simple.keys()
[1, 2, 3]
>>> simpleCopy = simple.copy()
>>> print simpleCopy.keys()
[1, 2, 3]
>>> simple.has_key( 1 )
1
>>> simpleCopy.clear()
>>> print simpleCopy
{}
>>> print simple
{1: 'one', 2: 'two', 3: 'three'}
>>> simple.get( 3 )
'three'
>>> simple.update( { 4: "four" } )
>>> print simple
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
You might also like to view...
The edge of the display screen in Windows 8 is commonly called the bezel
Indicate whether the statement is true or false
You can extend the multimedia impact of video by adding a(n) ____ to mark the frame where you want to trim a video, or identify other specific frames of interest.
A. template B. frame C. bookmark D. index