Can you create a class from a string name

Anna revanna at
Sat Mar 1 22:36:43 CET 2003

On Sat, 01 Mar 2003 20:45:31 +0000, Vivek Sawant wrote:

> Hi,
> is there a way to create a class object or an instance object for a
> class if you have the name of the class as a string at the runtime.
> For example, in Java you can create a 'Class' object as:
> Class.forname ('<classname>')
> Thanks.
> \vivek

I don't know about Java.... But, from your question, it sounds like you're
asking about an object factory.

def factory(aClass, *args):   
    return apply(aClass,args) 

so if you have a class Spam, that takes 2 args, during runtime, you can
tell it:

newobject=factory(Spam, arg1, arg2)


More information about the Python-list mailing list