Generic Python
Max M
maxm at mxm.dk
Mon Jun 24 05:48:47 EDT 2002
Uwe Mayer wrote:
> The problem is that I've got a base class and there should be many
> subclasses of it. Each subclass just overwriting an output method and
> serving as a template:
>
> class base1:
> def __init__(self):
> ...
> def output(self):
> ...
>
> class template1(base1):
> def output(self):
> ...
>
> class template2(base1):
> def ouptut(self):
> ...
>
> I need many, many of these "template" classes, so a sorter way would be
> to accustom the base class to take another argument which then outputs
> the right thing:
This problems begs for you to create a factory function::
def factory(objType, initValue):
classTable = {
'template1':template1
'template2':template2
}
return classTable[objType](initValue)
This way you don't have to know the specifics anout the class that your
create.
you would then just have to::
import templates
newObj = templates.factory('template2', 'Initial text')
regards Max M
More information about the Python-list
mailing list