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'}

Computer Science & Information Technology

You might also like to view...

Answer the following statements true (T) or false (F)

1. You cannot assign a base class reference to a derived class variable. 2. A method that accepts a base class variable as a parameter cannot accept a derived class variable as a parameter. 3. A class that is not intended to be instantiated but is to be used only as a base class is called a concrete class. 4. A statement that tries to use the new operator instantiate an abstract class will not compile. 5. If you want to create an abstract read-only property, leave out the set accessor.

Computer Science & Information Technology

he attribute specifying a blue background is

a. bgcolor=#000000 b. background=”blue” c. style=”background-color:blue” d. bgcolor=blue

Computer Science & Information Technology

Clipping is the process of transferring an item from a clipboard to a specific location in a document.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Endianness: Let’s say you want to write a program for comparing two strings. You have a choice of using a 32-bit byte-addressable Big-endian or Little-endian architecture to do this. In either case, you can pack 4 characters in each word of 32-bits. Which one would you choose and how will you write such a program? [Hint: Normally, you would do string comparison one character at a time. If you can do it a word at a time instead of a character at a time, that implementation will be faster.]

What will be an ideal response?

Computer Science & Information Technology