Can you create a class from a string name

Mark McEahern marklists at mceahern.com
Sat Mar 1 16:21:38 EST 2003


[Vivek Sawant]
> 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>')

#!/usr/bin/env python

class Junk:

    def __init__(self):
        print "I am Junk"

s = 'Junk'

j = locals()[s]()

// m
-






More information about the Python-list mailing list