How to instatiate a class of which the name is only known at runtime?

Max M maxm at mxm.dk
Wed Sep 10 09:12:55 CEST 2003


Max M wrote:

> 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)

That last line should read:

myObject = Factory('class2')

Max M





More information about the Python-list mailing list