question on creating class
Steven D'Aprano
steve at REMOVE.THIS.cybersource.com.au
Thu Jan 4 06:30:19 EST 2007
On Wed, 03 Jan 2007 23:27:57 -0800, wcc wrote:
> Hello,
>
> How do I create a class using a variable as the class name?
Try a "class factory".
def create_class(classname):
class Klass(object):
def __init__(self):
print "Creating object of %s..." % self.__class__.__name__
def method(self):
print "This is a method."
k = Klass
k.__name__ = classname
return k
>>> K = create_class("TestClass")
>>> obj = K()
Creating object of TestClass...
--
Steven.
More information about the Python-list
mailing list