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

How to create an object instance from a string??

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
Tian wrote:
I have a string:
classname = "Dog"


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

Jul 18 '05 #2

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

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

P: n/a
This is very useful, thanks!

Jul 18 '05 #5

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

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

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

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

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