Dynamic creation of an object instance of a class by name

A

Andre Meyer

Hi all

I have been searching everywhere for this, but have not found a solution, yet.

What I need is to create is an object that is an instance of a class (NOT a
class instance!) of which I only know the name as a string. This what I tried:

class A:
def __init__(self, id):
self.id = id
def getID(self):
print self.id

ca = new.classobj('A', (), {})
oa1 = ca()
oa2 = new.instance(ca)

oa1
<__main__.A instance at 0x007E8AF8>

oa1.getID()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: A instance has no attribute 'getID'

oa2
<__main__.A instance at 0x007E8AF8>

oa2.getID()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: A instance has no attribute 'getID'


Thus, both ways only produce a class instance, but NOT an object of that
class!!! How can I get a new object instance???
What else needs to be filled in for base_classes and __dict__ in new.classobj?


Any help is appreciated, this is driving me nuts!

kind regards
Andre
 
P

Peter Abel

Andre Meyer said:
Hi all

I have been searching everywhere for this, but have not found a solution, yet.

What I need is to create is an object that is an instance of a class (NOT a
class instance!) of which I only know the name as a string. This what I tried:

class A:
def __init__(self, id):
self.id = id
def getID(self):
print self.id

ca = new.classobj('A', (), {})
oa1 = ca()
oa2 = new.instance(ca)

oa1
<__main__.A instance at 0x007E8AF8>

oa1.getID()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: A instance has no attribute 'getID'

oa2
<__main__.A instance at 0x007E8AF8>

oa2.getID()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: A instance has no attribute 'getID'


Thus, both ways only produce a class instance, but NOT an object of that
class!!! How can I get a new object instance???
What else needs to be filled in for base_classes and __dict__ in new.classobj?


Any help is appreciated, this is driving me nuts!

kind regards
Andre

I'm not quite sure if I guess what you mean:
Is it?.... def __init__(self,id):
.... self.id=id
.... def getID(self):
.... print self.id
.... item

Or rather the following?

The first one evaluates the string 'A' to a class-object from
which you can instantiate.
The second one inherits classNEW() from A() with name 'classX'
with the namespace of globals().'A'

See http://www.python.org/doc/2.2.3/lib/module-new.html
....
classobj(name, baseclasses, dict)
This function returns a new class object, with name name, derived from
baseclasses (which should be a tuple of classes) and with namespace
dict.

Regards
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top