[Tutor] instantiating subclass by parameter

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Oct 28 23:52:32 EST 2003


> I'd rather have a dict with the desired object names and use that to
> call the factory function. Kinda like this (which doesn't work).
> 
>      create = {'alpha': 'altavista', 'beta': 'alltheweb'}
>      for key in create:
>          key = factory(create[key])


So why not bypass the factory and just do:

classes = {'AltaVista':AltaVista,'WebSearch':WebSearch,...}

a = classes['AltaVista']()
b = classes['WebSearch']()

after all classes are objects too...

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list