Driver for simple class SingleList. : Your List « Collections « Python Tutorial






class SingleList:
   def __init__( self, initialList = None ):
      self.__list = []  
      if initialList:
         for value in initialList:
            if value not in self.__list:
               self.__list.append( value )

   def __str__( self ):
      tempString = ""
      i = 0
      for i in range( len( self ) ):
         tempString += "%12d" % self.__list[ i ]
      return tempString
      
   def __len__( self ):
      return len( self.__list )

   def __getitem__( self, index ):
      return self.__list[ index ]

   def __setitem__( self, index, value ):
      if value in self.__list:
         raise ValueError, "List already contains value %s" % str( value )
      
      self.__list[ index ] = value

   def __eq__( self, other ):
      if len( self ) != len( other ):
         return 0  

      for i in range( 0, len( self ) ):

         if self.__list[ i ] != other.__list[ i ]:
            return 0

      return 1  

   def __ne__( self, other ):
      return not ( self == other )      



def getIntegers():
   size = 5

   returnList = []  # the list to return

   for i in range( size ):
      returnList.append(
         int( raw_input( "Integer %d: " % ( i + 1 ) ) ) )
      
   return returnList

integers1 = SingleList( getIntegers() )

integers2 = SingleList( getIntegers() )

print len( integers1 )
print integers1

if integers1 != integers2:
   print "They are not equal"

if integers1 == integers2:
   print "They are equal"

print integers1[ 0 ]
integers1[ 0 ] = 0
print integers1

duplicates = [ 1, 2, 2, 3, 4, 3, 6, 9 ]
print "List with duplicates is:", duplicates

single = SingleList( duplicates )
print single
print len( single )

if 4 in single:
   print "The value 4 was found in list"








9.6.Your List
9.6.1.Driver for simple class SingleList.