How to copy class objects?
fredrik at pythonware.com
fredrik at pythonware.com
Mon Feb 12 05:44:41 EST 2001
matthias oberlaender 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.
class Stack:
def push(...):
...
def pop(self):
...
class FloatStack(Stack):
def push(...):
assert isinstance(item, FloatType)
Stack.push(...)
class IntStack(Stack):
def push(...):
assert isinstance(item, IntType)
Stack.push(...)
or perhaps:
class Stack:
type = None
def push(...):
assert isinstance(item, self.type)
def pop(...):
...
class FloatStack(Stack):
type = FloatType
class IntStack(Stack):
type = IntType
the latter also allows you to configure instances on
the fly:
stack = Stack()
stack.type = TupleType
(as usual, Python works better if you use it to write
Python programs...)
Cheers /F
Sent via Deja.com
http://www.deja.com/
More information about the Python-list
mailing list