Definition of class SimpleDictionary. : Your Dictionary « Collections « Python Tutorial






class SimpleDictionary:
   def __getitem__( self, key ):
      return self.__dict__[ key ]

   def __setitem__( self, key, value ):
      self.__dict__[ key ] = value

   def __delitem__( self, key ):
      del self.__dict__[ key ]      

   def __str__( self ):
      return str( self.__dict__ )

   def keys( self ):
      return self.__dict__.keys()

   def values( self ):
      return self.__dict__.values()

   def items( self ):
      return self.__dict__.items()
   

simple = SimpleDictionary()
print "The empty dictionary:", simple

simple[ 1 ] = "one"
simple[ 2 ] = "two"
simple[ 3 ] = "three"
print "The dictionary after adding values:", simple

del simple[ 1 ] 
print "The dictionary after removing a value:", simple

print "Dictionary keys:", simple.keys()
print "Dictionary values:", simple.values()
print "Dictionary items:", simple.items()








9.5.Your Dictionary
9.5.1.Definition of class SimpleDictionary.