How to copy class objects?

Steve Purcell stephen_purcell at
Mon Feb 12 11:31:49 CET 2001

matthias.oberlaender at 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): = []
  def push(self, elem):
    assert type(elem) == self.elementType
  def pop(self):

class IntStack(TypedStack):
   elementType = IntType

class FloatStack(TypedStack):
   elementType = FloatType


Steve Purcell, Pythangelist
PythonTesting  = urlopen('')
PythonServlets = urlopen('')
Available for consulting and training.
"Even snakes are afraid of snakes." -- Steven Wright

More information about the Python-list mailing list