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

Dynamic creation of an object instance of a class by name

P: n/a
Hi all

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

What I need is to create 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
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
cn = 'A'
inst = eval('%s()' % cn)

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
Andre Meyer wrote:
I have been searching everywhere for this, but have not found a
solution, yet.

What I need is to create 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


Don't listen to the voices whispering "Use eval()" or "globals() are the way
to go", make it clean and explicit with a dictionary containing all classes
you want to expose:
class A: pass .... class B: pass .... class C: pass .... exposed = dict(A=A, B=B, C=C)
exposed["A"]()


:-)

Peter

Jul 18 '05 #3

P: n/a
> Don't listen to the voices whispering "Use eval()" or "globals() are the
way to go", make it clean and explicit with a dictionary containing all
classes you want to expose:


Out of curiosity - why not to use eval? having a dict like yours makes
things look like your average java factory pattern - some explicit
structure, to which access has to be guaranteed and that requires a
registration.

Of course, if you know that you will always only select the class from a
fixed num of classes, your approach ensures that no non-compliant classes
can be selected - but hey, we're dynamic here ;) You never can be totally
sure what you get....
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4

P: n/a
me***@acm.org (Andre Meyer) writes:
Hi all

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

What I need is to create an object that is an instance of a class (NOT a
class instance!)
I'm sorry, I can't for the life of me figure out what the (semantic)
difference is between "intsance of a class" and "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', (), {})
This creates a new class which will be called "A", displacing your
original class by the same name.
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!!!
Yes, they produce an instance of your newly created class.
How can I get a new object instance???
I think you mean to ask "How can I get a new instance of the class
whose name I have in a string?". In other words, you should be asking
yourself, "How can I get my hands on an object whose name[*] I have in a
string?" (One possible approach is shown below.)
What else needs to be filled in for base_classes and __dict__ in
new.classobj?


You can't do it that way ... that way you create a _new_ separate
class called "A".

Here's how you might achieve what you want:
class A: .... def __init__(self, id):
.... self.id = id
.... def getID(self):
.... print self.id
.... oa1 = globals()['A'](12345)
oa1 <__main__.A instance at 0x8190844> oa1.getID() 12345
[*] I'm a bit wary about using "name" here, because I don't really
mean the name of the class, but the name of a variable that
happens to refer to the class.

For example, consider (in the context established above)
B = A
del A
B.__name__ 'A' A.__name__

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'A' is not defined

So, while the name of the class is still "A", it is no longer
known by an identifier of that name.
Jul 18 '05 #5

P: n/a
Diez B. Roggisch wrote:
Don't listen to the voices whispering "Use eval()" or "globals() are the
way to go", make it clean and explicit with a dictionary containing all
classes you want to expose:


Out of curiosity - why not to use eval? having a dict like yours makes
things look like your average java factory pattern - some explicit
structure, to which access has to be guaranteed and that requires a
registration.

Of course, if you know that you will always only select the class from a
fixed num of classes, your approach ensures that no non-compliant classes
can be selected - but hey, we're dynamic here ;) You never can be totally
sure what you get....


It's probably a matter of taste, but most of the time the use of eval()
suggests more freedom than there actually is. Let's suppose there are 3
classes A, B, and C that will be used by your eval(), that limitation may
then be enforced in various parts of your code or in the documentation,
while I tend to see exposed = dict(...) as self-documenting. You can go to
the implementation of one class in the list to look up the interface.

To take an (almost) real-world example from the library, what would you
suppose the following to do?

klass = eval(klass, vars(logging))

What would you do to make the above accept your special-purpose handler
class? I think something like

availableHandlers = {...}

def registerHandler(name, klass, replace=False):
if not replace and name in availableHandlers:
raise NameClash
availableHandlers[name] = klass

formatterClass = availableHandlers[name]

is much clearer.

As a side note (not that I think this is important here), eval() is not the
fastest of functions:

$ timeit.py -s"class A: pass" -s"all=dict(A=A)" "all['A']"
10000000 loops, best of 3: 0.147 usec per loop
$ timeit.py -s"class A: pass" "eval('A')"
10000 loops, best of 3: 19.7 usec per loop
Peter

Jul 18 '05 #6

P: n/a
Right, ok, well, I do not assume to know what classes can be instantiated, thus
anything like accessing globals() is not likely to help, sorry. The eval()
version works pretty ok so far. I am develpoing a module of which other people
can make use of by developing their own subclasses, but they need to be
instantiated by my code. Eval() does the trick for me or is there a better way,
assuming you do not need to know which class it might be beforehand?
btw I LOVE dynamicity... ;-)

kind regards
Andre
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.