Dynamic creation of an object instance of a class by name

Peter Otten __peter__ at web.de
Thu Apr 8 02:47:15 EDT 2004


Andre Meyer wrote:

> Right, ok, well, I do not assume to know what classes can be instantiated,
> thus anything like accessing globals() is not likely to help, sorry. The
> eval() version works pretty ok so far. I am develpoing a module of which
> other people can make use of by developing their own subclasses, but they
> need to be instantiated by my code. Eval() does the trick for me or is
> there a better way, assuming you do not need to know which class it might
> be beforehand? btw I LOVE dynamicity... ;-)

You have to pass the name of the subclasses to your module somehow for your
code to know what to instantiate. Why don't you pass the subclasses
themselves instead?

Peter




More information about the Python-list mailing list