polymorph.py

class Object:
 def __init__(self):
  raise Exception, "attempting to instantiate ABSTRACT base class"

 def show(self):
  pass

class Number(Object):
 
 def __init__(self, value=0):
  self.__data = value

 def show(self):
  return "NUM("+str(self.__data)+")"

class String(Object):

 def __init__(self, value=""):
  self.__data = value

 def show(self):
  return "STRING("+self.__data+")" 

class Tuple(Object):

 def __init__(self, xvalue=0, yvalue=0):
  self.__data = (xvalue, yvalue) 

 def show(self):
  return "TUPLE("+str(self.__data[0])+","+str(self.__data[1])+")"

if __name__ == "__main__":
 pl=[Number(10),
     Number(999),
     String("hello"),
     Tuple(3,4),
     String("End")]

 print "\nthe list\n"
 print pl

 print "\ntraversing the list\n"
 for index in range(len(pl)):
  print pl[index].show()

 print "\ninstantiating Object\n"
 o=Object()


Generated by GNU enscript 1.6.1.