By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

question on creating class

P: n/a
wcc
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

Jan 4 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
You can always rename your defined clas afterwards

class TestClass:
pass

myClass = TestClass

--
Tõnis

On Jan 4, 9:27 am, "wcc" <wcc...@gmail.comwrote:
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
Jan 4 '07 #2

P: n/a
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...@gmail.comwrote:
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
Jan 4 '07 #3

P: n/a
wcc schrieb:
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
You call 'type' to create a new-style class. The signature is:
"type(name, bases, dict) -a new type".
>>def __init__(self):
.... print "Creating object of TestClass..."
....
>>def method1(self):
.... print "This is a method"
....
>>TestClass = type("TestClass", (), {"__init__": __init__, "method1": method1})
help(TestClass)
Help on class TestClass in module __main__:

class TestClass(__builtin__.object)
| Methods defined here:
|
| __init__(self)
|
| method1(self)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __dict__ = <dictproxy object>
| dictionary for instance variables (if defined)
|
| __weakref__ = <attribute '__weakref__' of 'TestClass' objects>
| list of weak references to the object (if defined)
>>TestClass()
Creating object of TestClass...
<__main__.TestClass object at 0x00AED0B0>
>>TestClass().method1()
Creating object of TestClass...
This is a method
>>>

Thomas

Jan 4 '07 #4

P: n/a
wcc kirjoitti:
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
I'm curious: what is the original problem that you are trying to solve?

The following works, but note that I'm no Python expert and if any of
the regular wizards around here says that doing these kind of things is
sick, I'm gonna agree 100% ;)

className = "TestClass2"

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

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

exec('class ' + className +
'''(object):
def __init__(self):
print "Creating object of TestClass2..."

def method1(self):
print "This is a method1 of TestClass2."
''')

if __name__ == "__main__":
o = TestClass()
o.method1()
o2 = TestClass2()
o2.method1()
o2 = locals()[className]()
o2.method1()
Jan 4 '07 #5

P: n/a
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.

Jan 4 '07 #6

P: n/a
wcc
Thanks for all replies. I'll just to have to figure our which suggested
method I should use. To answer Jussi's question, this is why I asked
the question. I have the book by Mark: Python Programming on Win32.
In Charpter 12: Advanced Python and COM there is a sample code named:
DynamicPolicy.py. It can be used to expose all methods from a python
module to a COM server which can be accessed later from other
languages, say VBA. It works great. There are quite a few modules that
I want to implement a COM server for. One of the step to create the
COM server is to define a class. Instead of revising this code for
each module, I wondered if I can create mutilple COM servers in one
program. I'll pass a module name and the corresponding class name to
some function (to be written), and the class will be created. Btw, I'm
novice to all this, hopefully my explanation isn't way off. Thank to
all again.

--
wcc

Jan 4 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.