question on creating class

wcc wccppp at gmail.com
Thu Jan 4 02:27:57 EST 2007


Hello,

How do I create a class using a variable as the class name?

For example, in the code below, I'd like replace the line

class TestClass(object):
with something like
class eval(className) (object):

Is it possible?  Thanks for your help.

className = "TestClass"

class TestClass(object):
    def __init__(self):
        print "Creating object of TestClass..."

    def method1(self):
        print "This is a method."

if __name__ == "__main__":
    o = TestClass()
    o.method1()

--
wcc




More information about the Python-list mailing list