Dynamic creation of an object instance of a class by name

Diez B. Roggisch deetsNOSPAM at web.de
Wed Apr 7 11:51:35 EDT 2004


> Don't listen to the voices whispering "Use eval()" or "globals() are the
> way to go", make it clean and explicit with a dictionary containing all
> classes you want to expose:

Out of curiosity - why not to use eval? having a dict like yours makes
things look like your average java factory pattern - some explicit
structure, to which access has to be guaranteed and that requires a
registration.

Of course, if you know that you will always only select the class from a
fixed num of classes, your approach ensures that no non-compliant classes
can be selected - but hey, we're dynamic here ;) You never can be totally
sure what you get....
-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list