469,271 Members | 1,466 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

How to create an object instance from a string??

How can I create an instance of an object from a string?

For example, I have a class Dog:
class Dog:
def bark(self):
print "Arf!!!"
I have a string:
classname = "Dog"

How can I create a instance of Dog from classname?
Is there any such methods like those in Java?

Jul 18 '05 #1
9 30025
Tian wrote:
I have a string:
classname = "Dog"


It's easier without strings:
classname = Dog
classname().bark() Arf!!!

Jul 18 '05 #2
On 19 Mar 2005 14:16:42 -0800, Tian <wa*********@gmail.com> wrote:
How can I create an instance of an object from a string?

For example, I have a class Dog:
class Dog:
def bark(self):
print "Arf!!!"

I have a string:
classname = "Dog"

How can I create a instance of Dog from classname?


If class Dog is in the same namespace:
dog_class = globals()[classname]
dog = dog_class()
dog.bark()

If class is declared in another file, e.g. "animals.py":
import animals
dog_class = getattr(animals, classname)
dog = dog_class()
dog.bark()
--
Ksenia
Jul 18 '05 #3
Tian wrote:
How can I create an instance of an object from a string?

For example, I have a class Dog:
class Dog:
def bark(self):
print "Arf!!!"
I have a string:
classname = "Dog"

How can I create a instance of Dog from classname?
Is there any such methods like those in Java?


You generally get a reference to the class object
(i.e. to Dog) using either globals(), locals(),
or getattr() as appropriate for the specific
situation at hand...

-Peter
Jul 18 '05 #4
This is very useful, thanks!

Jul 18 '05 #5
Hi !
Also :
classname = "Dog"
exec("b="+classname+"()")
b.bark()

or

classname = "Dog"
exec("cl="+classname)
b=cl()
b.bark()

Michel Claveau


Jul 18 '05 #6
Do Re Mi chel La Si Do wrote:
Also :
classname = "Dog"
exec("b="+classname+"()")
b.bark()

or
classname = "Dog"
exec("cl="+classname)
b=cl()
b.bark()


Ugh.

The statement exec (note: it's a statement, not a
function call as you imply with the above) is rarely
either required or good style.

In this case it's neither.

-Peter
Jul 18 '05 #7
Hi !
Sorry, but :

The statement exec does not have impact on the style.

Like with exefile or import (and other), the style is determined by the
contents. Rather judge "exec" (or import, or execfile) than the contents,
it is an error of causality.
@-salutations
--
Michel Claveau

Jul 18 '05 #8
Tian wrote:
How can I create an instance of an object from a string?

For example, I have a class Dog:

class Dog:
def bark(self):
print "Arf!!!"

def Factory(class_name):
classes = {
'Dog':Dog
}
return classes[class_name]
dog = Factory('Dog')()
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Jul 18 '05 #9
Hi !

Finally, it's the best solution.

Michel Claveau
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Steve Neill | last post: by
4 posts views Thread by Ray | last post: by
2 posts views Thread by John Hughes | last post: by
7 posts views Thread by MisterPete | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.