How to instatiate a class of which the name is only known at runtime?
Max M
maxm at mxm.dk
Wed Sep 10 03:08:18 EDT 2003
Marco Herrn wrote:
> I am writing a program that has to instantiate a class from which I
> don't know the name until runtime. That leads to two problems for me.
Is there any reason not to use a standard factory pattern for this? It
is the normal approach.
# Factory.py
import class1, class2, class3
def Factory(class_type):
if class_type='class1':
return class1()
elif class_type='class2':
return class2()
elif class_type='class2':
return class3()
# using the factory
from Factory import Factory
myObject = Factory(class2)
regards Max M
More information about the Python-list
mailing list