How to copy class objects?

Steve Purcell stephen_purcell at yahoo.com
Mon Feb 12 05:31:49 EST 2001


matthias.oberlaender at daimlerchrysler.com wrote:
> Ok, here's a more detailed example. I want a stack containing only ints. Then 
> I want a stack containing only floats. Extending the pattern to other types 
> should be straightforward. Of course, neither should FloatStack be a base 
> class of IntStack, nor vice versa. Indeed, new.classobj seems to offer a 
> possible solution.

This is really not doing the simplest possible thing. How about:


class TypedStack:
  elementType = None

  def __init__(self):
    self.data = []
    
  def push(self, elem):
    assert type(elem) == self.elementType
    self.data.append(elem)
  
  def pop(self):
    return self.data.pop()


class IntStack(TypedStack):
   elementType = IntType

class FloatStack(TypedStack):
   elementType = FloatType



-Steve

-- 
Steve Purcell, Pythangelist
PythonTesting  = urlopen('http://pyunit.sourceforge.net/')
PythonServlets = urlopen('http://pyserv.sourceforge.net/')
Available for consulting and training.
"Even snakes are afraid of snakes." -- Steven Wright




More information about the Python-list mailing list