Python class instantiation using name

holger krekel pyth at devel.trillke.net
Tue Dec 17 17:16:16 EST 2002


Amol P Dharmadhikari wrote:
> Hi All,
> 
> I have a question about python, which I did not find answered in
> the FAQs and tutorials.
> 
> Is it possible to create an instance of a class using its name?
> If so how?
> 
> So in general, if I have a string which contains the name of a class,
> how do I create an instance of that class?

    cls = eval(classname)

if the class is defined in the current module.  or

    cls = eval(classname, othermodule.__dict__)

if the class lives in another module.  

HTH,

    holger




More information about the Python-list mailing list