question on creating class

tonisk metsakuri at gmail.com
Thu Jan 4 02:36:11 EST 2007


Or if you have required class name in variable, then use:

class TestClass:
    pass

globals()[className] = TestClass

--
Tõnis

On Jan 4, 9:27 am, "wcc" <wcc... at gmail.com> wrote:
> 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